I''ve just been bodyslammed by a problem with has_and_belongs_to_many - as far as I can tell, if it doesn''t appear at the top of other relationship definitions it doesn''t seem to work right. For example: class Artist < ActiveRecord::Base has_and_belongs_to_many :genres has_many :albums has_many :videos has_many :cds has_many :collections end ..works fine but.. class Artist < ActiveRecord::Base has_many :albums has_many :videos has_many :cds has_many :collections has_and_belongs_to_many :genres end when I put it at the end of the list, there''s only one artist per genre for some reason. I switch it back and forth several times importing from an XML with the same results - the first resulting correct relationships (99 records in artists_genres), while the second resulted in an artists_genres table containing 7 records - one for each genre. The way the records were inserted was with something like this: artist = Artist.new( my_arguments ) genre = Genre.find_by_name( my_genre_name ) unless genre genre = Genre.new( :name => my_genre_name ) end artist.genres << genre artist.save I _think_ I''m doing everything right, but if not I''m sure someone will let me know ;).. but anyway, has anyone run into a problem like that with HABTM before? -Pawel -- Posted via http://www.ruby-forum.com/.
Pawel Szymczykowski wrote:> I''ve just been bodyslammed by a problem with has_and_belongs_to_many - > as far as I can tell, if it doesn''t appear at the top of other > relationship definitions it doesn''t seem to work right. For example: > > class Artist < ActiveRecord::Base > has_and_belongs_to_many :genres > has_many :albums > has_many :videos > has_many :cds > has_many :collections > end > > ..works fine but.. > > class Artist < ActiveRecord::Base > has_many :albums > has_many :videos > has_many :cds > has_many :collections > has_and_belongs_to_many :genres > end > > when I put it at the end of the list, there''s only one artist per genre > for some reason. I switch it back and forth several times importing from > an XML with the same results - the first resulting correct relationships > (99 records in artists_genres), while the second resulted in an > artists_genres table containing 7 records - one for each genre. > > The way the records were inserted was with something like this: > > artist = Artist.new( my_arguments ) > genre = Genre.find_by_name( my_genre_name ) > unless genre > genre = Genre.new( :name => my_genre_name ) > end > artist.genres << genre > artist.save > > I _think_ I''m doing everything right, but if not I''m sure someone will > let me know ;).. but anyway, has anyone run into a problem like that > with HABTM before?Yes, this gels with the problems reported in the last few days: http://www.ruby-forum.com/topic/48785 http://www.ruby-forum.com/topic/48816 Your discovery of the sensitivity to association declaration order is a big clue to what''s going wrong. As I reported before, I could not reproduce the problem using the simplest possible pair of HABTM-related models. I''ll add another association try again, though I''m not skilled at framework debugging. -- We develop, watch us RoR, in numbers too big to ignore.
Mark Reginald James wrote:> Yes, this gels with the problems reported in the last few days: > http://www.ruby-forum.com/topic/48785 > http://www.ruby-forum.com/topic/48816 > > Your discovery of the sensitivity to association declaration order > is a big clue to what''s going wrong. As I reported before, I > could not reproduce the problem using the simplest possible pair > of HABTM-related models. I''ll add another association try again, > though I''m not skilled at framework debugging.Oops - I must have missed those. It definitely seems to be a related issue. Thanks for pointing me in the right direction. I haven''t really started down the road of playing with internals yet but if there''s anything I can do to help, just let me know. Hopefully this gets ironed out soon. -Pawel -- Posted via http://www.ruby-forum.com/.