: autosave property of has_many unions broken in Rails 2.3.4?

Before posting this as an error for the rails command, I wanted to see if I was doing something wrong that might cause this behavior. In particular, the autosave property of has_many associations does not seem to work as per the docs.

For reference, here is the latest API documentation: http://api.rubyonrails.org/classes/Acti ... ation.html

Take a look at the One-to-Many section. I duplicated the code there exactly in the test application, and it does not work for me. In particular, the parent is updated, but the child is not.

My diagram is as follows:

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 as follows:

  class Post < ActiveRecord::Base has_many :comments, :autosave => true end class Comment < ActiveRecord::Base belongs_to :post end 

In the console, I run the following commands (post objects and comments are already in the database at this point):

  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 what I get for output:

  post.title # => "On the migration of ducks" post.comments.last.body # => "Actually, your article should be named differently." 

Further, looking at the logs, this is the only update statement that I see:

Post Update (0.6ms) UPDATE "posts" SET "updated_at" = '2010-01-18 23:32:39', "title" = 'About duck migration' WHERE "id" = 1

Thus, it seems that the save did not cascade to the comment object, which seems to me clearly broken. I tried this on two different systems running 2.3.4, and the behavior is repeatable.

Here's the weird part: if I call “post.comments” first, before trying to set the value, it works great! To be precise:

  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 

Now the result gives me the correct 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 the logs contain the correct update:

Update comments (0.3ms) UPDATE "comments" SET "updated_at" = '2010-01-18 23:44:43', "body" = 'Actually, your article should be named differently. [UPDATED]: You're right, thanks. WHERE "id" = 2

So it really looks violated to me. I assume this is a problem with how object references are handled, i.e. When an object is part of a selected collection, it saves a penalty, but does not, when it is pulled as a single object from the database. But before I send this to the Rails team as an error, I wanted to see if someone else ran into this problem, or if I was just doing something completely disinterested, which I didn’t see because I spent all day on this.

+7
ruby ruby-on-rails activerecord orm persistence
source share
1 answer

This is as often as expected, and the work around is quite simple:

 last_comment = post.comments.last last_comment.body = "[totally awesome dream hands]" last_comment.save 

Not very concise, but functional :)

0
source share

All Articles