我正在修改一个有赞助商和会员的申请,并希望增加一个协会,赞助商可以拥有许多可能是会员的联系人。会员可以成为许多赞助商的联系人。这会是一个很好的通过关系,联系人使用成员类吗?这是我尝试过的。
class Sponsor < ActiveRecord::Base
has_many :member_contacts
has_many :contacts, through: member_contacts, class_name: :member
end
class Contact < ActiveRecord::Base
has_many :member_contacts
has_many :sponsors, through: member_contacts
end
class MemberContact < ActiveRecord::Base
belongs_to :contact
belongs_to :sponsor
end
class Member < ActiveRecord::Base
has_many :subscriptions
has_many :groups, through: :subscriptions
has_many :member_notes
has_many :chapters, -> { uniq }, through: :groups
has_many :announcements, -> { uniq }, through: :groups
validates_uniqueness_of :email
end
sponsors_controller.rb
class SponsorsController < ApplicationController
def index
@sponsors = Sponsor.joins(:sponsor_sessions).all
end
def new
@sponsor = Sponsor.new
@sponsor.contacts.build
end
end
和form.html.haml
= f.collection_select :contacts, Member.all, :id, :full_name, { selected: @sponsor.contacts.map(&:id) }, { multiple: true }
当我尝试访问/赞助/新时,我得到
'uninitialized constant Sponsor::member'
指向sponsors_controller新方法中的行
@sponsor.contacts.build
有谁能让我知道我做错了什么?
答案 0 :(得分:1)
问题在于Sponsor
型号
has_many :contacts, through: member_contacts, class_name: :member
哪个应该是
has_many :contacts, through: member_contacts, class_name: 'Member'