Before I publish this like a bug towards the rails team, I needed to ascertain if I am doing a problem that might be leading to this behavior. Particularly, the :autosave property of has_many associations does not appear to become working according to the paperwork.
For reference, this is actually the most up to date API documentation: http://api.rubyonrails.org/classes/Acti … ation.html
Have a look in the "One-to-many Example" section. I've copied the code there exactly inside a test application, also it is not employed by me. Particularly, parents object is up-to-date, however the child object isn't.
My schema is the following:
create_table :posts do |t| t.string :title t.timestamps end create_table :comments do |t| t.text :body t.integer :post_id t.timestamps
My models are the following:
class Post < ActiveRecord::Base has_many :comments, :autosave => true end class Comment < ActiveRecord::Base belongs_to :post end
Within the console, I run the next instructions (the publish and comments objects happen to be within the DB at this time):
post = Post.find(1) post.title # => "The current global position of migrating ducks" post.comments.first.body # => "Wow, awesome info thanks!" post.comments.last.body # => "Actually, your article should be named differently." post.title = "On the migration of ducks" post.comments.last.body = "Actually, your article should be named differently. [UPDATED]: You are right, thanks." post.save post.reload
But this is exactly what I recieve for output:
post.title # => "On the migration of ducks" post.comments.last.body # => "Actually, your article should be named differently."
Further, searching within the logs this is actually the only update statement I see:
Publish Update (.6ms) UPDATE "posts" SET "up-to-date_at" = '2010-01-18 23:32:39', "title" = 'On the migration of ducks' WHERE "id" = 1
Therefore it seems the save did not cascade lower towards the comments object, which appears clearly damaged in my experience. I've attempted this on two different systems running 2.3.4 and also the behavior is repeatable.
Here's the strange part, though: Basically first call "publish.comments" before I attempt to create the worthiness, it really works fine! To become exact:
post.title = "On the migration of ducks" post.comments #Note that this line was not called above post.comments.last.body = "Actually, your article should be named differently. [UPDATED]: You are right, thanks." post.save post.reload
The output provides me with the right results:
post.title # => "On the migration of ducks" post.comments.last.body # => "Actually, your article should be named differently. [UPDATED]: You are right, thanks."
And also the logs retain the correct update:
Comment Update (.3ms) UPDATE "comments" SET "up-to-date_at" = '2010-01-18 23:44:43', "body" = 'Actually, your article ought to be named in a different way. [Up-to-date]: You're right, thanks.' WHERE "id" = 2
Which means this really looks damaged in my experience. I am speculating it's a trouble with the way in which object references are handled, i.e. that when the item is a component of the allotted collection it saves fine but doesn't save when it's drawn like a single object in the database. Before I submit this towards the Rails team like a bug I needed to ascertain if anybody else had encounter this problem, or maybe I am just doing something completely boneheaded that I am not seeing because I have spent all day long about this.
This really is so common they can be expected, and also the deal with is straightforward enough:
last_comment = post.comments.last last_comment.body = "[totally awesome dream hands]" last_comment.save
Not so succinct, but functional :)