Add record to a has_and_belongs_to_many relationship

前端 未结 4 1006
天涯浪人
天涯浪人 2021-01-31 04:00

I have two models, users and promotions. The idea is that a promotion can have many users, and a user can have many promotions.

class User < ActiveRecord::Bas         


        
4条回答
  •  南方客
    南方客 (楼主)
    2021-01-31 04:16

    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.

提交回复
热议问题