Habtm relation and dependent => :destroy


#1

Hi all ,
I have a habtm relation between 2 tables:

  • users
  • rights

If there a way to affect a destroy dependance on those 2 tables only for
the last instance? In my opinion, dependent => :destroy is not the
solution because this delete all the linked instance.

For example, if I have:

in User model this:

  • “Joooooo”
  • “Jeannnn”
  • “Tommmm”

in Right model this:

  • “Write”
  • “Read”

in the habtm relation this:

  • “Joooooo” can “Write”
  • “Joooooo” can “Read”
  • “Jeannnn” can “Read”

I would like to be able to delete “Read” in Right model, and automaticly
deleting “Jeannnn” user too, because this user have no other right. But
without deleting “Joooooo” user (because he remains have a right named
“Write”).

Thanks in advance.


#2

Panda B. wrote:

For example, if I have:

  • “Jeannnn” can “Read”

I would like to be able to delete “Read” in Right model, and automaticly
deleting “Jeannnn” user too, because this user have no other right. But
without deleting “Joooooo” user (because he remains have a right named
“Write”).

Perhaps something like:

class Right
after_destroy do
User.find(:all, :uniq => true, :joins => :rights,
:conditions => ‘rights.id is NULL’).each(&:destroy)
end
end


Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com