class Vendor < ActiveRecord::Base
attr_accessible :name, :address_attributes
has_many :campaigns
has_many :clients, :through => :campaigns
end
class Client < ActiveRecord::Base
attr_accessible :name
has_many :campaigns
has_many :vendors, :through => :campaigns
end
class Campaign < ActiveRecord::Base
attr_accessible :name, :vendor_id, :client_id, :start_date, :end_date
belongs_to :client
belongs_to :vendor
end
这是新的竞选形式
=f.错误消息
=f.标签:名称
=f.text\u字段:名称
=f.label:客户id,“客户”
%比尔
=f.collection\u select(:client\u id,client.all,:id,:name,{:prompt=>”请选择客户端“},{:class=>”客户端(列表“})
现在,联想法
@vendor.clients
campaign
加入模型。
如果竞选表格是空的,就没有了
clients
@供应商.客户
collection_select
在新的活动形式中,我必须能够选择属于供应商的客户。所以,我不得不把
Client.all
打电话。
因此,获取/创建/关联客户和供应商,并
@供应商.客户
尽管
运动
不是创建的,我必须在供应商和客户之间添加另一个多对多关联,对吗?
如果我真的创建了habtm协会,它会互相冲突,对吗?
class Vendor < ActiveRecord::Base
attr_accessible :name, :address_attributes
has_many :campaigns
has_many :clients, :through => :campaigns
has_and_belongs_to_many :clients
end
class Client < ActiveRecord::Base
attr_accessible :name
has_many :campaigns
has_many :vendors, :through => :campaigns
has_and_belongs_to_many :vendors
end
我该怎么解决这个问题?因为现在,如果我这么做了,
@供应商.客户
或
@client.vendors
,调用哪个关联?
那个有
has_and_belongs_to_many
或
has_many: .., :through => ..