Add record to a has_and_belongs_to_many relationship

前端 未结 4 1047
天涯浪人
天涯浪人 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条回答
  •  萌比男神i
    2021-01-31 04:16

    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

提交回复
热议问题