How to specify a polymorphic association in a fixture
Asked Answered
H

1

7

I have this fixture:

resource:
  name: my resource
  owner: user_1 (User)

Resource is defined like this:

class Resource < ActiveRecord::Base
  belongs_to :owner, :polymorphic => true

User is defined like this:

class User < ActiveRecord::Base
  has_many :resources, :as => :owner

When I run the test I get an error that the column 'owner' is not defined in the table. What am I doing wrong?

Hypertension answered 9/8, 2011 at 4:15 Comment(2)
Did you have the wrong migration?Autopsy
possible duplicate of Unit testing a polymorphic association with fixturesStopcock
R
1

The fixture should have owner_id and owner_type. For example: owner_id as 1 and owner_type as "User"

Rusk answered 31/5, 2016 at 12:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.