This problem is related to this question: Hashing an IP for saving
I have a model called Post. I have to pass in the IP address during post creation. I was told not to override the initialize(). So I used a factory method as suggested here: how to override new method for a rails model:
#Post.rb model
def self.new_with_ip(ip, attributes={})
self.new(attributes['one_day_id'] = do_some_conversion_on(ip))
end
However this does not get invoked, because Post is nested within a Discussion, and the nested form will not call this factory method. How can I make the form to invoke this instead of the traditional Post.new()?
If this is being passed through as a nested attribute of a form then you would have to override the
posts_attributes=method of theDiscussionmodel:Of course you’re going to need to modify that a little if you’re going to be getting nested posts in an update kind of fashion, as you’ll want to update existing posts rather than creating new ones. Sounds like a good exercise in learning 🙂