如果我没什么进展,我希望有人能帮上忙。我有以下设置,从上到下,我将删除一个伪代码。我想保留多态模型的勘探,因为rcs_prospecting和ara_prospecting都使用此模型中的字段,也有自己的字段。
contact.rb
class Contact < ActiveRecord::Base
has_one :ara_prospecting
has_one :rcs_prospecting
....
end
ara_prospecting.rb
class AraProspecting < ActiveRecord::Base
belongs_to :contact
has_one :prospecting, as: :prospectable
accepts_nested_attributes_for :prospecting, :reject_if => :all_blank, :update_only => true
end
rcs_prospecting.rb
class RcsProspecting < ActiveRecord::Base
belongs_to :contact
has_one :prospecting, as: :prospectable
accepts_nested_attributes_for :prospecting, :reject_if => :all_blank, :update_only => true
end
prospecting.rb
class Prospecting < ActiveRecord::Base
belongs_to :prospectable, polymorphic: true
belongs_to :user
end
我要做的是通过ara_prospecting和rcs_prospecting关联从联系人模型创建一个与用户模型关联的has_one。从本质上讲我希望
has_one :ara_user, through...(ara_prospecting -> prospecting -> user)
has_one :rcs_user, through...(rcs_prospecting -> prospecting -> user)
我看过各种文章,尝试过不同的东西,但似乎无法破解它。
任何帮助都将不胜感激。
提前谢谢你
发布于 2015-04-11 21:37:27
我没有使用has_one :through
关联,而是完全删除了ara_prospecting
和rcs_prospecting
。
class Contact < ActiveRecord::Base
has_many :prospecting
....
end
class Prospecting < ActiveRecord::Base
belongs_to :contact
scope :ara, -> {where(category: "ara")}
scope :rcs, -> {where(category: "rcs")}
end
在Prospecting
迁移中,添加t.string :category
,并在创建对象时为其分配ara
或rcs
。我定义的作用域可以让你很容易地将它们分开。
https://stackoverflow.com/questions/29577897
复制相似问题