I did a real basic github project here that demonstrates the problem. Basically, when I create a new comment, it is saved as expected; when I update an existing comment, it is not saved. However, this is not what the docs say for :autosave => true... they say the opposite. Here is the code:
class Post < ActiveRecord::Base
has_many :comments,
:autosave => true,
:inverse_of => :post,
:dependent => :destroy
def comment=(val)
obj=comments.find_or_initialize_by(:posted_at=>Date.today)
obj.text=val
end
end
class Comment < ActiveRecord::Base
belongs_to :post, :inverse_of=>:comments
end
Now in the console I am testing:
p=Post.create(:name=>'How to groom your unicorn')
p.comment="That cool!"
p.save!
p.comments
p.comment="But how to you polish the rainbow?"
p.save!
p.comments
Why not? What am I missing?
Please note that if you do not use "find_or_initialize", it works in the same way that ActiveRecord respects the association cache, otherwise it reloads comments too often, throwing changes. those. this implementation works
def comment=(val)
obj=comments.detect {|obj| obj.posted_at==Date.today}
obj = comments.build(:posted_at=>Date.today) if(obj.nil?)
obj.text=val
end
, , , . , , , .