首页 文章

Rails has_one通过has_many不起作用

提问于
浏览
2

有没有人有运气让has_one在同一个模型中经历has_many关系 . 我一直在

ActiveRecord :: HasOneThroughCantAssociateThroughCollection:不能有has_one:通过关联

似乎很容易获取一组has_many结果并通过特定键对其进行过滤,并将其称为has_one关系 .

使用rails 3.2.12

这是我现在的协会参与是一个不同的模式 .

has_one :original_participation, :through => :participation

has_one :original_participant, :through => :original_participants, :foreign_key => "organization_id"

has_many :original_participants,
  :through => :original_participation,
  :source => :participants

我需要经历这最后一次关联,然后通过organization_id对其进行过滤 .

ActiveRecord :: HasOneThroughCantAssociateThroughCollection:不能有has_one:通过关联'Surveys :: Participant#original_participant'其中:通过关联'Surveys :: Participant#original_participants'是一个集合 . 请改为:through选项中指定has_one或belongs_to关联 .

1 回答

  • 2

    使用has_one,您不需要:通过不需要中介关系 .

    如果我认为我知道你要做什么,你就有了一个测试层次结构:

    has_one test_parent, :class_name => "Test", foreign_key: "child_test"

    has_many tests

    给他们打电话:

    @my_array_of_children = tests

    @my_parents_id = test.id

    等等

相关问题