Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rail 3.2.2/Devise: deprecation warning with rspec

I recently upgraded an app to rails 3.2.2.

I'm using Factory_girl

Factory.sequence :name do |n| "name-#{n}" end

Factory.define :user do |u| u.first_name{ Factory.next(:name) }
u.last_name { |u| 'last_' + u.first_name } u.password 'secret'
u.password_confirmation { |u| u.password } u.sequence(:email) { |i| "user_#{i}@example.com" }

end

and this simple test

specify { Factory.build(:user).should be_valid }

generate the following warning

DEPRECATION WARNING: You're trying to create an attribute user_id'. Writing arbitrary attributes on a model is deprecated. Please just use attr_writer` etc. (called from block (2 levels) in at...

How can I get rid of it?

like image 900
Alpha Avatar asked Mar 03 '12 07:03

Alpha


2 Answers

It's probably because you haven't prepared/migrated your test database with updated column definitions, thus it thinks you're trying to arbitrarily set the attribute.

Run rake db:test:prepare to make sure it's updated.

Here's the source code of that method, where you can see Rails checks for the column or attribute first, then warns if they're not found.

like image 77
trisweb Avatar answered Oct 11 '22 10:10

trisweb


I've met the same warning with the following code:

Ad model:

class Ad < ActiveRecord::Base
    belongs_to :user
end

Factories:

FactoryGirl.define do 
    factory :ad do
        association :user
    end
end

FactoryGirl.define do 
    factory :user do
        first_name {Factory.next(:first_name)}
        last_name {Factory.next(:last_name)}
        email {|x| "#{x.first_name}.#{x.last_name}#{Factory.next(:count)}@test.com"}
        password Forgery(:basic).password
        confirmed_at Date.today << 10
    end
end

Test

require 'spec_helper'

describe Ad do
    before(:each) do
        @ad = Factory.build(:ad)
    end

    "it is not valid without a user"
end

Running the test gave me a similar error.

Adding

attr_accessor :user

to the Ad model fixed the warning.

I hope it helps.

like image 40
Cristian Stănescu Avatar answered Oct 11 '22 10:10

Cristian Stănescu