Forum: Ruby on Rails Avoiding HABTM field clobbering?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 CSN (Guest)
on 2005-12-24 21:54
(Received via mailing list)
>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/
4a4d97879b09aca01f3036055ef4813a?d=identicon&s=25 Brian Green (Guest)
on 2005-12-25 01:13
(Received via mailing list)
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
137039860fa6b38713e1349da8a4137d?d=identicon&s=25 Marc Love (Guest)
on 2005-12-25 01:28
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 Green wrote:
>
> class Reader
>   has_many :readings
> end
>
> class Article
>   has_many :readings
> end
>
> class Reading
>   belongs_to :reader
>   belongs_to :article
> end
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2005-12-25 17:27
(Received via mailing list)
On 12/24/05, Marc Love <marc.love@herbank.com> 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.
This topic is locked and can not be replied to.