Is there an equivalent to the collection.build command in Ruby on Rails that builds a relationship between two already existing model objects? For instance, if I have a User model and a Contact model (connected both ways by a has_many_and_belongs_to) and I want to add a user to Contact’s collection of users and a contact to the user’s collection of contacts, is there any easy method that simply builds this connection for me? Or do I have to write out
contact.users << some_user
contact.save
some_user.contacts << contact
some_user.save
I believe that:
is enough.
According to the Rails Guide to Active Record Associations: