Forum: Ruby on Rails question about saving associations when using has_many :thro

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D0802a5ae80c2a40a9ef07967c6dc362?d=identicon&s=25 Mike G. (random52k)
on 2006-06-13 21:34
(Received via mailing list)
I've got a User model and a Book model.  A user is required to submit
books each month to be reviewed by other users. I have the following
relationships defined:

# the join model
book_review.rb
  belongs_to :user
  belongs_to :book
end

book.rb
  # each book is reviewed by many users. This allows us to get the
BookReview
  # objects associated with this book
  has_many :book_reviews, :dependent => true

  # this :through association allows us to retrieve the users who have
  # been assigned to review this book
  has_many :reviewing_users, :through => :book_reviews, :source => :user

  # each book is submitted by a single user
  belongs_to :user
end

user.rb
  # each user must submit a number of books each month
  has_many :submitted_books, :class_name => "Book", :dependent =>
:delete_all

  # each user has a list of books that they are required to review
  has_many :book_reviews, :dependent => true

  # this :through association allows us to retrieve the books the user
  # is required to review
  has_many :books_to_review, :through => :book_reviews, :source => :book
end

Now, when a user submits a book, I must assign the book to be reviewed
by 5 different users.  I've created an after_save callback in book.rb
which does the following:

    users.each do |user|
      user.books_to_review << self
      user.save!
    end

unfortunately, when I add a new book model to the list of
books_to_review, it doesn't actually insert the book model into the
book_reviews table.  I can of course do the following instead:

   users.each do |user|
      book_review = BookReview.new("book_id" => self.id)
      user.book_reviews << book_review
      user.save!
    end

I was hoping that rais would be smart enough in the first case to add
a new book record to the book_reviews table, filling in the book_id
and user_id parameters, but instead it doesn't even save it.  I can,
however, print out the contents of user.books_to_review and sure
enough, it contains the new book that I've added, but I just can't
figure out how to get rails to save this information to the table.

Can anyone tell me why rails isn't saving the books_to_review list to
the user model in the first example?  And how I can fix it? (or should
I just rely on using the second method instead?)

Thanks,

Mike

P.S. here is the schema for the book_reviews table:

book_reviews
+-------------+----------+------+-----+---------+----------------+
| Field       | Type     | Null | Key | Default | Extra          |
+-------------+----------+------+-----+---------+----------------+
| id          | int(11)  | NO   | PRI |         | auto_increment |
| book_id     | int(11)  | YES  |     |         |                |
| user_id     | int(11)  | YES  |     |         |                |
| total_score | int(11)  | YES  |     |         |                |
| created_at  | datetime | YES  |     |         |                |
| updated_at  | datetime | YES  |     |         |                |
| reviewed_at | datetime | YES  |     |         |                |
+-------------+----------+------+-----+---------+----------------+
This topic is locked and can not be replied to.