【发布时间】:2015-07-26 01:52:39
【问题描述】:
我正在修改一个包含赞助商和成员的应用程序,并希望添加一个协会,让赞助商可以拥有许多可能是成员的联系人。成员可以是许多赞助商的联系人。这会是一个 has_many through 关系,其中联系人使用成员类吗?这是我尝试过的。
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 }
当我尝试访问 /sponsors/new 时,我得到了
'uninitialized constant Sponsor::member'
指向sponsors_controller新方法中的行
@sponsor.contacts.build
谁能告诉我我做错了什么?
【问题讨论】:
-
你能发布你的
Member模型吗?
标签: ruby-on-rails-4 has-many-through