Relationship not saving

Hi All,

I have a one to many relationship between images and gifts (one image
can be assigned to multiple gifts):

class ProductImage < ActiveRecord::Base
has_many :gifts

class Gift < ActiveRecord::Base
belongs_to :product_image

I’m trying to associate an image with a gift, and for the life of me
ActiveRecord will not associate them. Here is some test code and the
associated output:

gift.product_image = ProductImage.find_or_create()
p “PRODUCT IMAGE IS: #{gift.product_image}”
gift.product_image.save
p “PRODUCT IMAGE IS: #{gift.product_image}”
gift.save
p “PRODUCT IMAGE IS: #{gift.product_image}”
gift.reload
p “PRODUCT IMAGE IS: #{gift.product_image}”
gift.save
p “PRODUCT IMAGE IS: #{gift.product_image}”

and the output is:

“PRODUCT IMAGE IS: #ProductImage:0xb73e6a40
“PRODUCT IMAGE IS: #ProductImage:0xb73e6a40
"PRODUCT IMAGE IS: "
"PRODUCT IMAGE IS: "
"PRODUCT IMAGE IS: "

AS soon as I go and save the gift, the image gets wiped out - the
association never makes it into the database. Any ideas how this could
be?

Thanks!
Tom

Tom L. wrote:

AS soon as I go and save the gift, the image gets wiped out - the
association never makes it into the database. Any ideas how this could
be?

I found out what line of code was calling this… I had an unexpected
gift.reload call that was being made elsewhere in the code. I still
don’t know why that caused the problem, but it’s resolved now.

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