Sometimes when you get puzzled by what Rails is doing, you really just need to understand what Ruby is doing.
For example, given this simple code to get an attribute value:
1 2 3 4
Beginners are often stumped by why this code does not set an attribute value:
1 2 3 4 5 6
What’s going on?
In the first method,
some_attribute is actually a method call which gets the
attribute value of the record. This works in Rails ActiveRecord due to the Ruby
feature of method_missing which allows some code to run when a method is called
that does not exist.
In the second method, a local variable called some_attribute is getting assigned. There is no call to method_missing, as this is a variable assignment!
The correct code should have been:
1 2 3 4 5