Jacob Waller
Jacob Waller

Reputation: 4229

Rails has_one through has_many not working

Has anybody had any luck having a has_one go through a has_many relationship in the same model. I keep getting

ActiveRecord::HasOneThroughCantAssociateThroughCollection: Cannot have a has_one :through association

It seems like it would be easy take a set of has_many results and filter it down by a specific key and call it a has_one relationship.

Using rails 3.2.12

Here is my associations right now participation is a different model.

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

I need to go through this last association and filter it down by organization_id.

ActiveRecord::HasOneThroughCantAssociateThroughCollection: Cannot have a has_one :through association 'Surveys::Participant#original_participant' where the :through association 'Surveys::Participant#original_participants' is a collection. Specify a has_one or belongs_to association in the :through option instead.

Upvotes: 2

Views: 3072

Answers (1)

lilwupster
lilwupster

Reputation: 893

With has_one you should not need :through There is no need for an intermediary relationship.

If I think I know what you're trying to do you have a hierarchy of tests:

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

has_many tests

to call them:

@my_array_of_children = tests

@my_parents_id = test.id

etc.

Upvotes: 2

Related Questions