I am having a problem with the ''<<'' operator and a HABTM relationship. Basically, I have two entities, Photo and PhotoTag (think Flickr-like app). Photo: has_and_belongs_to_many :photo_tags PhotoTag: has_and_belongs_to_many :photos Ok, no problem. During execution, I want to add a tag to a photo, so I have code like this: @new_photo.photo_tags << @photo_tag In this example, @new_photo is an uncommitted new instance of Photo and @photo_tag is a fetched PhotoTag. Right after this call, we save @new_photo. It saves all other fields but the FK to the photo tag join table is null. Logging shows that @photo_tag is not nil. I was under the impression that ''<<'' was how I added an object to the relation... Am I wrong? Anyway, can someone help me figure out if I am off the reservation or what? Cheers, H
What happens when you save the @new_photo before adding photo_tags to it?
Here''s a working example from a "test fixture" script in one
of my apps:
disaster = Disaster.create(:disaster_name => ''Hurricane
Wilson'',
:start_date => Date.new(2005,8,1))
areas = Area.find(:all, :conditions => "state_code =
''FL''")
areas.each { |area| area.disasters<<disaster }
Disaster.create makes a new record and then saves it. Notice how both
sides of the HABTM in my example are already present in the DB, before
they are associated with each other.
--Wilson.
On 10/6/05, Hunter Hillegas
<lists-HAWAbpnI61OZ1JSuHaJ1sQC/G2K4zDHf@public.gmane.org>
wrote:> I am having a problem with the ''<<'' operator and a
HABTM relationship.
>
> Basically, I have two entities, Photo and PhotoTag (think Flickr-like app).
>
> Photo:
> has_and_belongs_to_many :photo_tags
>
> PhotoTag:
> has_and_belongs_to_many :photos
>
> Ok, no problem.
>
> During execution, I want to add a tag to a photo, so I have code like this:
>
> @new_photo.photo_tags << @photo_tag
>
> In this example, @new_photo is an uncommitted new instance of Photo and
> @photo_tag is a fetched PhotoTag.
>
> Right after this call, we save @new_photo. It saves all other fields but
the
> FK to the photo tag join table is null.
>
> Logging shows that @photo_tag is not nil.
>
> I was under the impression that ''<<'' was how I
added an object to the
> relation... Am I wrong?
>
> Anyway, can someone help me figure out if I am off the reservation or what?
>
> Cheers,
> H
I am having a problem with the ''<<'' operator and a HABTM relationship. Basically, I have two entities, Photo and PhotoTag (think Flickr-like app). Photo: has_and_belongs_to_many :photo_tags PhotoTag: has_and_belongs_to_many :photos Ok, no problem. During execution, I want to add a tag to a photo, so I have code like this: @new_photo.photo_tags << @photo_tag In this example, @new_photo is an uncommitted new instance of Photo and @photo_tag is a fetched PhotoTag. Right after this call, we save @new_photo. It saves all other fields but the FK to the photo tag join table is null. Logging shows that @photo_tag is not nil. I was under the impression that ''<<'' was how I added an object to the relation... Am I wrong? Anyway, can someone help me figure out if I am off the reservation or what? Cheers, H