Soft delete in HABTM tables

Couple tips: - don't capitalize association names. It might not affect anything at first, but it will eventually bite you when the inflector gets confused.

- as noted by others, don't optimize early. And even if you're trying to optimize early, why would 'delete profile' be a bottleneck? Surely views are going to be much, much more common...

- 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

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

Matt Jones wrote:

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

Now I got what I needed exactly. Thank you so much for guiding me in the proper way :slight_smile: