Add record to a has_and_belongs_to_many relationship
user = User.find(params[:id])promotion = Promotion.find(params[:promo_id])user.promotions << promotion
user.promotions
is an array of the promotions tied to the user.
See the apidock for all the different functions you have available.
You can do just
User.promotions = promotion #notice that this will delete any existing promotions
or
User.promotions << promotion
You can read about has_and_belongs_to_many
relationship here.
This is also useful
User.promotion.build(attr = {})
so, promotion object saves, when you save User object.
And this is
User.promotion.create(attr = {})
create promotion you not need to save it or User model