pseudosudo
pseudosudo

Reputation: 1980

FactoryGirl table and column have the same name, unable to save values

I have an Image model with a :text column named image and am unable to save the image field using FactoryGirl.

Here are the relevant piece of code:

image_spec.rb:
let(:image) { FactoryGirl.create(:image) }
subject { image }
it { should be_valid }


factories.rb:
factory :image do
    image "123456789.jpg"
end


image.rb:
attr_accessible :image
validates :image, presence: true

When I run the tests I get the error

Failure/Error: let(:image) { FactoryGirl.create(:image) }
 ActiveRecord::RecordInvalid:
   Validation failed: Image can't be blank

for all three of the lines in the image_spec.rb above.

I've tried applying How to initialize a column named 'sequence' with FactoryGirl but I couldn't get it working. (FactoryGirl creations for my other models work fine)

How do I assign to columns that have the same name as their table?

Upvotes: 0

Views: 474

Answers (1)

arieljuod
arieljuod

Reputation: 15848

try this:

FactoryGirl.define do
  factory :image do |i|
    i.some_attr 'xxx'
    i.image "123456789.jpg"
  end
end

that way you are actually calling the image= method of the image object, the other way factory girls thinks you are trying to create an association

(you have really used a confusing name for that column anyway :P, image_path or something like that would be better)

Upvotes: 1

Related Questions