I have a few classes. One of them, articles, can have other articles linked to it. Is the best way to model this with a belongs_to_and_has_many, ie class Article belongs_to_and_has_many :articles ... end So i can then say (eg in a list view) for article in @article.articles #do something with article end Will this work? Assuming this is the best way to do it, in the join table, which will be called articles_articles, i''m going to have to rename one or both of the columns from the default of article_id (since i can''t have two fields ith the same name). How do i tell rails the new column names? Appreciate any advice... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Max Williams wrote: > > Assuming this is the best way to do it, in the join table, which will be > called articles_articles, i''m going to have to rename one or both of the > columns from the default of article_id (since i can''t have two fields > ith the same name). How do i tell rails the new column names? > > Appreciate any advice...I did something like this for old application (done twice to make it two way): class Company has_and_belongs_to_many :corporate_shares_held_in, :class_name => ''Company'', :join_table => ''company_share_holders'', :foreign_key => ''company_id'', :association_foreign_key => ''share_holder_company_id'' has_and_belongs_to_many :corporate_share_holders, :class_name => ''Company'', :join_table => ''company_share_holders'', :foreign_key => ''share_holder_company_id'', :association_foreign_key => ''company_id'' end You should be able to use has_many :through though - try searching google "self referential has_many through". Cheers Luke -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> class Company > has_and_belongs_to_many :corporate_shares_held_in, > :class_name => ''Company'', > :join_table => ''company_share_holders'', > :foreign_key => ''company_id'', > :association_foreign_key => ''share_holder_company_id''Thanks - i actually just did something very similar and it worked - i set up join a table called article_links, where links are articles as well. Then, like you say above: has_and_belongs_to_many :links, :class_name => "Article", :association_foreign_key => "link_id", :join_table => "articles_links" Seems to work great, and it''s much nicer talking about "link in @article.links" than "article in @article.articles" which would have been confusing :) cheers! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Max, I have the same situation and model it like this:
class Article...
has_and_belongs_to_many :related_articles,
:class_name => "Article",
:join_table => "related_articles",
:association_foreign_key => "related_article_id",
:foreign_key => "article_id"
The related_articles table just has article_id and related_article_id
(no id column required)
Regards,
--Kip
On Jul 26, 12:22 am, Max Williams
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> I have a few classes. One of them, articles, can have other articles
> linked to it. Is the best way to model this with a
> belongs_to_and_has_many, ie
>
> class Article
> belongs_to_and_has_many :articles
> ...
> end
>
> So i can then say (eg in a list view)
>
> for article in @article.articles
> #do something with article
> end
>
> Will this work?
>
> Assuming this is the best way to do it, in the join table, which will be
> called articles_articles, i''m going to have to rename one or both
of the
> columns from the default of article_id (since i can''t have two
fields
> ith the same name). How do i tell rails the new column names?
>
> Appreciate any advice...
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---