Rails Has_many Association: Leveraging Genre_ids For Efficient Updates
has_many association in Rails gives 17 methods. Focus on collection_singular_ids=(ids) part which allows mass-assignment of genres for a book.
As explained in Rails association reference, defining a has_many association gives 17 methods. We'll focus on the collection_singular_ids=(ids) part. class Book has_many :book_genres has_many :genres, through: :book_genres end class Genre has_many :book_genres has_many :books, through: :book_genres end # the tie model class BookGenre belongs_to :book belongs_to :genre end Given a book, we'd get a genre_ids= (from has_many :genres). This method is very powerful. It's designed in such a way that you can use the value as-is straight from controller params...