2010-12-04 18 views
5
class Membership < ActiveRecord::Base 
    belongs_to :role 
    belongs_to :user 
end 

class User < ActiveRecord::Base 
    has_many :roles, :through => :memberships 
end 

class Role < ActiveRecord::Base 
    has_many :users, :through => :memberships 
end 

और मेरे देखेंसंघ नहीं मिल सका, रेल 3

<% for role in Role.find(:all) %> 
     <div> 
     <%=check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role) %> 
     <%=role.name%> 
     </div> 
    <% end %> 

मैं अपने दृश्य पर अगले त्रुटि मिल गया है - संघ नहीं मिल सका: मॉडल उपयोगकर्ता में सदस्यता और मैं नहीं कर सकता समझ में ऐसा क्यों होता है ..

उत्तर

15

आप, जो स्पष्ट रूप से करने के लिए has_many :memberships जरूरत है इस तरह:

class User < ActiveRecord::Base 
    has_many :memberships 
    has_many :roles, :through => :memberships 
end 

class Role < ActiveRecord::Base 
    has_many :memberships 
    has_many :users, :through => :memberships 
end 

उसमें जोड़ें, और आपको ऊपर और चलना चाहिए।

+0

धन्यवाद, आप आदमी हैं! – paxer

0

मैं कारण मिल गया है,

मैं अपने उपयोगकर्ता और भूमिका मॉडल के लिए

has_many :memberships 

जोड़ने की आवश्यकता होगी।

वैसे भी धन्यवाद! :)

+1

um.. सैम रिची का जवाब पहले से ही दिखाता है – Zabba

संबंधित मुद्दे