Rails has_one通过has_many不起作用

时间:2013-06-26 21:00:22

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.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',其中:through association“Surveys :: Participant#original_participants”是一个集合。请改为:through选项中指定has_one或belongs_to关联。

1 个答案:

答案 0 :(得分: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