I'd like to override the setter for an association, but write_attribute() isn't working - probably because that method only works for database columns.
我想覆盖关联的setter,但write_attribute()不起作用 - 可能是因为该方法仅适用于数据库列。
I have tried super(), but that doesn't work either (didn't think it would... but it was worth a guess).
我已经尝试过super(),但这也不起作用(不认为它会...但值得一猜)。
How do I override the setter? Here is what I am trying to do:
如何覆盖setter?这是我想要做的:
def parent=(value)
# this line needs to be changed
write_attribute(:parent, value)
if value.subject.start_with?('Re:')
self.subject = "#{value.subject}"
else
self.subject = "Re: #{value.subject}"
end
self.receivers << value.sender
end
4 个解决方案
#1
11
What worked for me is the following:
对我有用的是以下内容:
def parent=(new_parent)
# do stuff before setting the new parent...
association(:parent).writer(new_parent)
end
#2
3
I found one way to do it, but I am disturbed by it:
我找到了一种方法,但我对它感到不安:
alias_method :old_parent=, :parent=
def parent=(value)
self.old_parent = value
if value.subject.start_with?('Re:')
self.subject = "#{value.subject}"
else
self.subject = "Re: #{value.subject}"
end
self.receivers << value.sender
end
One thing I don't necessarily like about Rails is that whenever you want to do something that is out of the norm just a bit - but not unreasonable by any means - the "how" is very different than what your intuition would come up with.
关于Rails,我不一定喜欢的一件事是,无论何时你想要做一些超出常规的事情 - 但不是任何方式都不合理 - “如何”与你的直觉所带来的非常不同。
It's not a problem when you know the exceptions, but when you're learning, this sort of irregularity and inconsistency on how to do things makes it harder to learn - not easier.
当你知道异常时,这不是问题,但是当你在学习时,这种不规则和如何做事情的不一致使得学习变得更加困难 - 而不是更容易。
Java might be initially harder to learn, but it's way more consistent. Your intuition can take you a lot further once you think in Java. This is not true once you think in Rails. Rails is about memorization of methods to call and memorization on how to do things. In java, you can reason it out a lot more... and intellisense fills in the rest.
Java可能最初难以学习,但它更加一致。一旦你在Java中思考,你的直觉可以带你进一步发展。一旦你在Rails中思考,这就不是真的。 Rails是关于记忆调用和记忆如何做事的方法。在java中,你可以推理出更多...而intellisense填补其余部分。
I'm just disappointed. This is a reoccurring pattern for me - I want do something that is just "a little more complex" than the framework examples... and the "how" is inconsistent and takes 30 minutes or maybe even hours to locate and find the answer for it.
我只是很失望。这对我来说是一个反复出现的模式 - 我想做一些比框架示例“更复杂”的事情......而“如何”是不一致的,需要30分钟甚至几个小时来找到并找到答案它。
#3
3
In Rails 4.2.1 doc:
在Rails 4.2.1 doc中:
# Association methods are generated in a module that is included into the model class,
# which allows you to easily override with your own methods and call the original
# generated method with +super+. For example:
#
# class Car < ActiveRecord::Base
# belongs_to :owner
# belongs_to :old_owner
# def owner=(new_owner)
# self.old_owner = self.owner
# super
# end
# end
#4
0
Instead of
def parent=(value)
write_attribute(:parent, value)
end
Couldn't you just do:
你不能这样做:
def parent=(parent)
parent_id = parent.id
end
#1
11
What worked for me is the following:
对我有用的是以下内容:
def parent=(new_parent)
# do stuff before setting the new parent...
association(:parent).writer(new_parent)
end
#2
3
I found one way to do it, but I am disturbed by it:
我找到了一种方法,但我对它感到不安:
alias_method :old_parent=, :parent=
def parent=(value)
self.old_parent = value
if value.subject.start_with?('Re:')
self.subject = "#{value.subject}"
else
self.subject = "Re: #{value.subject}"
end
self.receivers << value.sender
end
One thing I don't necessarily like about Rails is that whenever you want to do something that is out of the norm just a bit - but not unreasonable by any means - the "how" is very different than what your intuition would come up with.
关于Rails,我不一定喜欢的一件事是,无论何时你想要做一些超出常规的事情 - 但不是任何方式都不合理 - “如何”与你的直觉所带来的非常不同。
It's not a problem when you know the exceptions, but when you're learning, this sort of irregularity and inconsistency on how to do things makes it harder to learn - not easier.
当你知道异常时,这不是问题,但是当你在学习时,这种不规则和如何做事情的不一致使得学习变得更加困难 - 而不是更容易。
Java might be initially harder to learn, but it's way more consistent. Your intuition can take you a lot further once you think in Java. This is not true once you think in Rails. Rails is about memorization of methods to call and memorization on how to do things. In java, you can reason it out a lot more... and intellisense fills in the rest.
Java可能最初难以学习,但它更加一致。一旦你在Java中思考,你的直觉可以带你进一步发展。一旦你在Rails中思考,这就不是真的。 Rails是关于记忆调用和记忆如何做事的方法。在java中,你可以推理出更多...而intellisense填补其余部分。
I'm just disappointed. This is a reoccurring pattern for me - I want do something that is just "a little more complex" than the framework examples... and the "how" is inconsistent and takes 30 minutes or maybe even hours to locate and find the answer for it.
我只是很失望。这对我来说是一个反复出现的模式 - 我想做一些比框架示例“更复杂”的事情......而“如何”是不一致的,需要30分钟甚至几个小时来找到并找到答案它。
#3
3
In Rails 4.2.1 doc:
在Rails 4.2.1 doc中:
# Association methods are generated in a module that is included into the model class,
# which allows you to easily override with your own methods and call the original
# generated method with +super+. For example:
#
# class Car < ActiveRecord::Base
# belongs_to :owner
# belongs_to :old_owner
# def owner=(new_owner)
# self.old_owner = self.owner
# super
# end
# end
#4
0
Instead of
def parent=(value)
write_attribute(:parent, value)
end
Couldn't you just do:
你不能这样做:
def parent=(parent)
parent_id = parent.id
end