Setting foreign keys, yet record.association_name returning nil

Please help me to understand why this piece of rspec code is behaving
this
way. I am setting product_id to my record, and it is asserting true, but
yet record.product not returning anything.

product = FactoryGirl.create(:product)

post :create, {:tshirt =>
FactoryGirl.attributes_for(:tshirt).stringify_keys.merge(“product_id”=>
product.id.to_s) } #successful creation
expect(Tshirt.last.product_id).to eq(product.id) # asserts true
puts Tshirt.last.product #prints nothing
expect(Tshirt.last.product.size).to eq(product.size) # exception!!! it
says, product = nil


Md. Sadaf N. (@sadaf2605 https://twitter.com/sadaf2605)
www.sadafnoor.com

Also posted at stackoverflow:
http://stackoverflow.com/questions/32414734/setting-foreign-keys-at-rails-yet-record-association-name-returning-nil

( Please don’t downvote )

2015-09-05 21:22 GMT+06:00 Sadaf N. [email protected]:

puts Tshirt.last.product #prints nothing
expect(Tshirt.last.product.size).to eq(product.size) # exception!!! it
says, product = nil


Md. Sadaf N. (@sadaf2605 https://twitter.com/sadaf2605)
www.sadafnoor.com


Md. Sadaf N. (@sadaf2605 https://twitter.com/sadaf2605)
www.sadafnoor.com

good point but nope, they are not nil

2015-09-06 10:39 GMT+06:00 Jim Ruther N. [email protected]:

2015-09-05 21:22 GMT+06:00 Sadaf N. [email protected]:

expect(Tshirt.last.product_id).to eq(product.id) # asserts true

Md. Sadaf N. (@sadaf2605 https://twitter.com/sadaf2605)

https://groups.google.com/d/msgid/rubyonrails-talk/CAAJ2eVqf%2BfSe7o%3DiA-PE3Sp_Bk23b-eNCiLumYvqB1iPXwiZQQ%40mail.gmail.com?utm_medium=email&utm_source=footer

“Ruby on Rails: Talk” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected]
To post to this group, send email to [email protected]
To view this discussion on the web visit

https://groups.google.com/d/msgid/rubyonrails-talk/CAJ8y7Vf6ryhm6mA94zvPbxZdTuW7xMYUgkTv%2BtNmh0R6qLXQOQ%40mail.gmail.com

https://groups.google.com/d/msgid/rubyonrails-talk/CAJ8y7Vf6ryhm6mA94zvPbxZdTuW7xMYUgkTv%2BtNmh0R6qLXQOQ%40mail.gmail.com?utm_medium=email&utm_source=footer

.
For more options, visit https://groups.google.com/d/optout.


Md. Sadaf N. (@sadaf2605 https://twitter.com/sadaf2605)
www.sadafnoor.com

On 6 September 2015 at 07:54, Sadaf N. [email protected] wrote:

good point but nope, they are not nil

What do the following show?
puts TShirt.last.inspect
puts product.inspect
puts Tshirt.last.product.inspect

and copy/paste the start of t_shirt.rb showing the associations

Colin

On Saturday, September 5, 2015, Sadaf N. [email protected] wrote:

this way. I am setting product_id to my record, and it is asserting true,

Check how product is defined in the tshirt model. Also check the value
of
product.id, it may be nil for all we know and tshirt.product_id is also
nil
so they’re equal.

<javascript:_e(%7B%7D,‘cvml’,‘[email protected]’);>

.
To post to this group, send email to [email protected]
<javascript:_e(%7B%7D,‘cvml’,‘[email protected]’);>.
To view this discussion on the web visit

https://groups.google.com/d/msgid/rubyonrails-talk/CAAJ2eVqf%2BfSe7o%3DiA-PE3Sp_Bk23b-eNCiLumYvqB1iPXwiZQQ%40mail.gmail.com

https://groups.google.com/d/msgid/rubyonrails-talk/CAAJ2eVqf%2BfSe7o%3DiA-PE3Sp_Bk23b-eNCiLumYvqB1iPXwiZQQ%40mail.gmail.com?utm_medium=email&utm_source=footer

.
For more options, visit https://groups.google.com/d/optout.


Sent from Gmail mobile

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