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.
I have tried super(), but that doesn’t work either (didn’t think it would… but it was worth a guess).
How do I override the setter? Here is what I am trying to do:
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
I found one way to do it, but I am disturbed by it:
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.
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.
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.