I trying to test a polymorphic association.
I am using RSpec 3.0.2 and shoulda-matchers 2.6.2. My Rails version is: 4.1.4 and Ruby 2.1.2
My First Model: app/models/address.rb
class Address < ActiveRecord::Base
belongs_to :addressable, polymorphic: true
end
My Second Model: app/models/user.rb
class User < ActiveRecord::Base
has_many :addresses, as: :addressable
end
My third model: app/models/company.rb
class Company < ActiveRecord::Base
has_many :addresses, as: :addressable
end
For Address table, I have this migration:
class CreateAddresses < ActiveRecord::Migration
def change
create_table :addresses do |t|
t.string :description
t.references :addressable, polymorphic: true
t.timestamps
end
end
end
And for my first specification, I have: spec/models/address_spec.rb
require 'rails_helper'
RSpec.describe Address, :type => :model do
it { should belong_to :addressable }
end
But when I try to run the specification I have this error:
Failures:
1) Address should belong to addressable Failure/Error: it { should belong_to :addressable } Expected Address to have a belongs_to association called addressable (Address does not have a addressable_id foreign key.) # ./spec/models/address_spec.rb:4:in `block (2 levels) in '
Generated table by migration:
Can You please help me identifying where I am missing. I just looked from google coming to stackoverflow, but not solution.