Forum: Ruby on Rails habtm relation and dependent => :destroy

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.
Panda B. (Guest)
on 2009-01-25 18:24
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.
Mark Reginald J. (Guest)
on 2009-01-26 18:01
(Received via mailing list)
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
This topic is locked and can not be replied to.