Issue when creating object with FactoryGirl

Hi Experts,

I am facing a weird issue. I learn that I can override default
attributes while instantiaing factory girl objects

https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md

I have a model, pasted below

when I instantiate the object overriding tc_user method, tc_user_id
becomes nil. Isn’t it supposed to pick up tc_user_id from my factory.

I am straching my head for 2 days. please help. Thanks in advance.

Thanks

Pradeep

models/user.rb

class User < ActiveRecord::Base

Include default devise modules. Others available are:

:confirmable, :lockable, :timeoutable and :omniauthable

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

Setup accessible (or protected) attributes for your model

attr_accessible :email, :password, :password_confirmation,
:remember_me
attr_accessible :tc_user_id, :tc_user , :created_timestamp,
:approved, :admin_user, :email,
:manager_id, :sso_id, :profile_image, :notes, :group_id,
:city_id, :functional_area, :medium_group,
:pay_id, :users_org, :group
has_many :est_claims, :primary_key => :tc_user_id, :foreign_key =>
:tc_user_id
has_many :est_notes, :primary_key => :tc_user_id, :foreign_key =>
:tc_user_id

validates :tc_user_id, :tc_user, :email, :manager_id, :group_id,

:pay_id, :users_org, presence: true

belongs_to :group
belongs_to :city
has_many :subordinates, class_name: "User", foreign_key:

“manager_id”
belongs_to :manager, class_name: “User”
end

====

factories/user.rb

Defines a new sequence

FactoryGirl.define do
sequence :email do |n|
“person#{n}_#{DateTime.now}@example.com
end
end

FactoryGirl.define do
factory :group do
name “USER”
end

end

This will guess the User class

FactoryGirl.define do
factory :user do
tc_user “John D.”
email
password 12345678
manager_id 1
tc_user_id { Faker::Number.number(6) }
group
pay_id 3
approved true
admin_user false
created_timestamp { DateTime.now }
end

This will use the User class (Admin would have been guessed)

factory :admin, class: User do
tc_user “Admin User”
email
approved true
tc_user_id 31735
password 12345678
manager_id 31735
group
pay_id 31735
admin_user true
created_timestamp { DateTime.now }
end
end

===========

1.9.3p484 :002 > u = FactoryGirl.build(:user)
(0.2ms) BEGIN
SQL (144.8ms) INSERT INTO “groups” (“created_at”, “description”,
“name”, “notes”, “updated_at”) VALUES ($1, $2, $3, $4, $5) RETURNING
“id” [[“created_at”, Tue, 14 Apr 2015 16:41:41 IST +05:30],
[“description”, nil], [“name”, “USER”], [“notes”, nil], [“updated_at”,
Tue, 14 Apr 2015 16:41:41 IST +05:30]]
(12.8ms) COMMIT
=> #<User id: nil, tc_user_id: 188214, tc_user: “John D.”,
created_timestamp: “2015-04-14 11:11:41”, approved: true, admin_user:
false, updated_timestamp: nil, notes: nil, group_id: 48, email:
“person1_2015-04-14T16:41:41+05:[email protected]”, sso_id: nil,
profile_image: nil, manager_id: 1, city_id: nil, functional_area: nil,
medium_group: nil, pay_id: 3, users_org: “Timescity”,
encrypted_password:
“$2a$10$o7meA4Au7/CbH/CpL41WduDhl8xyiq4NXrf1tolrVvGF…”,
reset_password_token: nil, reset_password_sent_at: nil,
remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil,
last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil>
1.9.3p484 :003 > u = FactoryGirl.build(:user, tc_user: “Rahul”)

(0.2ms) BEGIN
SQL (0.6ms) INSERT INTO “groups” (“created_at”, “description”,
“name”, “notes”, “updated_at”) VALUES ($1, $2, $3, $4, $5) RETURNING
“id” [[“created_at”, Tue, 14 Apr 2015 16:41:52 IST +05:30],
[“description”, nil], [“name”, “USER”], [“notes”, nil], [“updated_at”,
Tue, 14 Apr 2015 16:41:52 IST +05:30]]
(8.0ms) COMMIT

=> #<User id: nil, tc_user_id: nil, tc_user: “Rahul”,
created_timestamp: “2015-04-14 11:11:52”, approved: true, admin_user:
false, updated_timestamp: nil, notes: nil, group_id: 49, email:
“person2_2015-04-14T16:41:52+05:[email protected]”, sso_id: nil,
profile_image: nil, manager_id: 1, city_id: nil, functional_area: nil,
medium_group: nil, pay_id: 3, users_org: “Timescity”,
encrypted_password:
“$2a$10$98r4coszG7/tBKWOV2MBceO.BhiCEiT5OMHsht/pNjIB…”,
reset_password_token: nil, reset_password_sent_at: nil,
remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil,
last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil>
1.9.3p484 :004 >

====

rspec -v
3.2.2

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs