Ruby on rails Rails 4通过关联多态

Ruby on rails Rails 4通过关联多态,ruby-on-rails,nested,polymorphic-associations,Ruby On Rails,Nested,Polymorphic Associations,我希望有人能帮助我,但我进展很快。我有以下设置,自上而下,我将删除一个虚假代码。我希望保留勘探的多态模型,因为rcs_勘探和ara_勘探都使用该模型中的字段,并且也有自己的字段 contact.rb class Contact < ActiveRecord::Base has_one :ara_prospecting has_one :rcs_prospecting .... end 我看过各种文章,尝试过不同的东西,但似乎无法破解 任何帮助都将不胜感激 提前感谢您而不是

我希望有人能帮助我,但我进展很快。我有以下设置,自上而下,我将删除一个虚假代码。我希望保留勘探的多态模型,因为rcs_勘探和ara_勘探都使用该模型中的字段,并且也有自己的字段

contact.rb

class Contact < ActiveRecord::Base
  has_one :ara_prospecting
  has_one :rcs_prospecting

  .... 
end
我看过各种文章,尝试过不同的东西,但似乎无法破解

任何帮助都将不胜感激


提前感谢您

而不是使用
has\u one:通过
关联,我将删除
ara\u prospecting
rcs\u 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
class联系人{其中(类别:“ara”)}
范围:rcs,->{其中(类别:“rcs”)}
结束

Prospecting
迁移中,添加
t.string:category
,并在创建对象时为其分配
ara
rcs
。我定义的作用域可以让您轻松地将它们分开

谢谢你的建议。prospecting模型是rcs_prospecting和ara_prospecting“继承”的多态模型,但这些模型中的每一个都有自己独特的字段。不确定您的建议在这种情况下是否有效?嗯,如果
rcs
ara
模型每个只有几个字段,您可以将它们移动到
prospecting
模型。当然,您总是会有一些
nil
(或者可能为空)值,但它会起作用。如果它们有一组字段(比如,每个字段6个或更多),那么将它们组合起来可能不是最好的主意。但无论哪种方法都会奏效。谢谢你的帮助。我想坚持目前的方法,因为到目前为止,所有的工作都很好,这也是一个学习不同技术的好方法。协会通过每个模型引用它,但最好知道一种方法,从联系人模型中获得直接关系。再次感谢
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
class Prospecting < ActiveRecord::Base
  belongs_to :prospectable, polymorphic: true 
  belongs_to :user 
end
has_one :ara_user, through...(ara_prospecting -> prospecting -> user)
has_one :rcs_user, through...(rcs_prospecting -> prospecting -> user)
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