Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a polymorphic association in a fixture

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?

like image 647
Yoni Baciu Avatar asked Aug 09 '11 04:08

Yoni Baciu


People also ask

How is polymorphic association set up in Rails?

The basic structure of a polymorphic association (PA)sets up 2 columns in the comment table. (This is different from a typical one-to-many association, where we'd only need one column that references the id's of the model it belongs to). For a PA, the first column we need to create is for the selected model.

What is polymorphic association in Rails?

Polymorphic relationship in Rails refers to a type of Active Record association. This concept is used to attach a model to another model that can be of a different type by only having to define one association.

What exactly are harnesses and fixtures in the Ruby?

14) What exactly are Harnesses and Fixtures in the Ruby? These are basically the supporting codes with the help of which the users can easily write and can run the test cases. With the help of a rake, the users can then simply proceed with the automated tests.

What are RSpec fixtures?

Fixtures allow you to define a large set of sample data ahead of time and store them as YAML files inside your spec directory, inside another directory called fixtures. When you're test sweep first starts up RSpec will use those fixture files to prepopulate your database tables.


1 Answers

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

like image 112
Sakthi Gurunathan Avatar answered Sep 27 '22 17:09

Sakthi Gurunathan