On Feb 19, 11:12 pm, Preethi Sivakumar <rails-mailing-l...@andreas-
s.net> wrote:
> Matt Jones wrote:
>
> > - finally, while what you're asking about was possible in older
> > versions of Rails
> > (but seriously deprecated since about 2.0), I believe that the support
> > has been
> > removed from 2.3. The best practice now is to use a join model with
> > has_many :through.
> > Message me if you're not sure how to set that up.
>
> > --Matt Jones
>
> > On Feb 19, 2:17?am, Preethi Sivakumar <rails-mailing-l...@andreas-
>
> Thanks for your tips Jones.
>
> So, do you mean to say that, if I want to use a join model i've to use
> it using
> has_many and through relationship?
> if I'm not wrong, should it be like this?
> ----------------------------------------------------
> Model Profile
> has_many :users, through=>profile_user_mapping
> Model Users
> has_many :profiles, through=>profile_user_mapping
>

You've almost got it - it should look like this:

- in profile.rb:
has_many :profile_user_mappings
has_many :users, :through => :profile_user_mappings

- in user.rb:
has_many :profile_user_mappings
has_many :profiles, :through => :profile_user_mappings


> and one more question, is it not a good practice to use a mapping table
> without a model? should i use it only using has_many :through
> relationship?

The has_and_belongs_to_many stuff works, but it isn't capable of
expanding.
For example, if you wanted to add the kind of 'soft delete' you were
thinking
about at the beginning, habtm wouldn't work. With has_many :through,
you can
add the flag to ProfileUserMapping and then change
the :profile_user_mappings
assocation to:

has_many :profile_user_mappings, :conditions => { :deleted => false }

...or similar, and now the :profiles and :users associations will only
find records
joined by a profile_user_mapping with deleted = false.

--Matt Jones
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to