Rails idiom to avoid duplicates in has_many :through

前端 未结 8 1840
难免孤独
难免孤独 2020-12-07 12:09

I have a standard many-to-many relationship between users and roles in my Rails app:

class User < ActiveRecord::Base
  has_many :user_roles
  has_many :ro         


        
8条回答
  •  失恋的感觉
    2020-12-07 13:01

    This will create only one association in the database even if called multiple times Refer rails guide.

    user.roles=[Role.first] 
    

提交回复
热议问题