Overwriting default accessors

Hi all,

Coming from the Java & Hibernate world, I was confused when trying out
this piece of line of code:
class Task < ActiveRecord::Base
def status=(status)
status = ‘P’
write_attribute(:status, status)
end
end

I suspect that the value of :status in the database would be P,
because I presume rails would set the attribute value first before
saving it to the database. But instead I am getting this error:
ActiveRecord::StatementInvalid: Mysql::Error: Column ‘status’ cannot be
null

So what does overwriting default accessors used for in real life apps?
And how do we set the value before it is saved to the database in
Rails?

Thank you very much in advance for the insights.


Certified Scrum Master
http://twitter.com/scrum8 | http://blog.scrum8.com |
http://jobs.scrum8.com

Joshua P. wrote:

Hi all,

Coming from the Java & Hibernate world, I was confused when trying out
this piece of line of code:
class Task < ActiveRecord::Base
def status=(status)
status = ‘P’
write_attribute(:status, status)
end
end

This won’t quite work. But before I try to improve it…what are you
trying to do? Set status to P no matter the argument that the function
is called with?

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]