0

I followed the Restauranlty tutorial at rubyonrailstutor.com and everything worked perfectly. Now I'm trying to implement the same tests in my project, which has more models with associations between them and I'm getting errors.

I've tried researching this problem, and even came across a few questions on this site - most notably this one and this one.

I have the following models:

class Album < ActiveRecord::Base
  belongs_to :artist

  validates_presence_of :title
  validates_presence_of :no_of_tracks

end

class Artist < ActiveRecord::Base
  has_many :albums
end

And I have the following factories:

FactoryGirl.define do
  factory :artist do
    name "MyString"
    image "MyString"
    bio "MyString"
  end
end

FactoryGirl.define do
  factory :album do
    title "MyString"
    no_of_tracks 0
    artist
  end
end

Apart from following the Restaurantly tutorial, this is my first time testing so I also looked at this guide for setting up factory girl for models with associations.

My test is:

describe Album do
  subject(:album) { FactoryGirl.build :album, title: nil, artist: nil,
    no_of_tracks: nil }
  it { expect(album.valid?).to be_false }
end

But when I run it it fails with the error: can't write unknown attribute artist_id.

The questions I mentioned above suggested that perhaps my database has not been properly migrated but I've run both

bundle exec rake db:migrate RAILS_ENV=test

and

bundle exec rake db:test:prepare

to no avail. As I said, I'm very, very new to testing so if anyone could point me in the right direction I'd be grateful.

2 Answers 2

1

Everything looks fine. Do you really have a column artist_id in your albums table ?

Sign up to request clarification or add additional context in comments.

1 Comment

I can't believe it was that simple! I'd named my column artist by mistake instead of artist_id. Changed it and it all works fine now. Thank you! I'll accept you answer as soon as SO let me :0
0

I suspect that albums table in your database does not have artist_id foregin key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.