Avoiding HABTM field clobbering?


#1

From the docs:

“Note that any fields in the join table will override
matching field names in the two joined tables. As a
consequence, having an “id” field in the join table
usually has the undesirable result of clobbering the
“id” fields in either of the other two tables.”

I do have an ID field in the join table, and as a
result it’s clobbering the ID field of the associated
table. It appears I can specify my own “finder_sql”.
Any other way? Perhaps a :select option would be nice?

thanks
csn


Yahoo! for Good - Make a difference this year.
http://brand.yahoo.com/cybergivingweek2005/


#2

I take it there must be some other data in this join table that you’re
trying to keep track of–otherwise there would be no need for an id
field.
In that case, maybe you should think about turning the habtm relation
into
an intermediate Model, which has a belongs_to relationship with each of
the
original models. Somebody talked about that just a week ago or so on
this
list. Something like this, I think:

class Reader
has_many :readings
end

class Article
has_many :readings
end

class Reading
belongs_to :reader
belongs_to :article
end

hth
Brian


#3

The disadvantage to this method being that you can’t do @article.readers
or @reader.articles. Of course this will be changing with the
introduction of polymorphic associations in the next version of Rails:

class Reader
has_many :readings
has_many :articles, :through => :readings
end

class Article
has_many :readings
has_many :readers, :through => :readings
end

class Reading
belongs_to :reader
belongs_to :article
end

Brian G. wrote:

class Reader
has_many :readings
end

class Article
has_many :readings
end

class Reading
belongs_to :reader
belongs_to :article
end


#4

On 12/24/05, Marc L. removed_email_address@domain.invalid wrote:

The disadvantage to this method being that you can’t do @article.readers
or @reader.articles. Of course this will be changing with the
introduction of polymorphic associations in the next version of Rails:

You can do it, just manually. (i.e. create the Article#readers and
Reader#articles methods by hand.) It’s what I’m doing now.