class Wall < ActiveRecord::Base
has_many :shelves
end
class Shelf < ActiveRecord::Base
has_many :book
belongs_to :wall
end
class Book < ActiveRecord::Base
belongs_to :shelf
end
CREATE TABLE `walls` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE `shelves` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`wall_id` INT(11) NULL DEFAULT NULL,
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE `books` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`shelf_id` INT(11) NULL DEFAULT NULL,
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL,
PRIMARY KEY (`id`)
)
I want to move a book from one shelf to another, so I write…
book.shelf = new_shelf
but this doesn’t taken care of all the associations automatically.
When I query the old shelf for books, it returns the book I supposedly moved to another shelf.
what am I missing?
Call save after doing
book.shelf = new_shelfYou also have to call reload on the former shelf if it was pulled from the db before the update takes place.