Reputation: 1
I'm trying to override a initialize in a ActiveRecord model, I just saw one thing that I could not understand what was happening there. I wrote this initialize method:
def initialize params={}
super params
@data = Date.strptime(params[:data], '%d/%m/%Y') if not params[:data].nil?
self.number = generate_contract_number(params[:unit]) if not params[:unit].nil?
self
end
Given the generate_contract_number works and Date.strptime works as well. My question is: Why when I do self.number= the number is set and when I do @number= the number is not set. But when I do just the same with @contract_date= it works, and the self.contract_date= is set automatically?
Thanks
Upvotes: 0
Views: 189
Reputation: 160291
Don't override ActiveRecord's initialize
; use an after_initialize
callback.
For more details, see this SO post.
I don't see anything about @contract_date
, so I'm not sure what you mean. Without the model definition it's tough to say more, we don't know what @data
or @contract_date
is, or what "working" means. Bear in mind that an ActiveRecord's DB attributes aren't simple @
-style attributes.
Upvotes: 2