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?
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.
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.
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.
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.
The fixture should have owner_id and owner_type. For example: owner_id as 1 and owner_type as "User"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With