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:
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?
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?
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.