remco wrote:> Hello,
> I have a nice case for active record experts :0)
>
> i have the following situation:
> I have: customers users and groups and an acccess table which determines
> what group has access to what customer:
>
> this is what i have in my current (php) application
>
> customers:
> id
> name
> ....
>
> users
> id
> name
> ......
>
> groups
> id
> name
> ......
>
>
> groupmemberships
> id
> user_id
> group_id
>
> accessrights
> id
> group_id
> customer_id
>
>
> if someone wants to see all of his customers, i perform this query:
>
>
> select .... from customers where customer.id=accessrights.customer_id
> and accessrights.group_id=groupmemberships.group_id and
> groupmemberships.user_id=$currentuser
>
>
> i guess i can do something simular in rails, but are there better
> methods utilising active record?
This is a job for has_many :through. You''ll want to create join model
classes that embody the relationships between the other models. I''d
name
them Membership and Permission.
class Membership < ActiveRecord::Base
belongs_to :group
belongs_to :user
end
class Right < ActiveRecord::Base
belongs_to :group
belongs_to :customer
end
class Group < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :memberships
has_many :rights
has_many :customers, :through => :rights
end
class User < ActiveRecord::Base
has_many :memberships
has_many :groups, :through => :memberships
end
class Customer < ActiveRecord::Base
has_many :rights
has_many :groups, :through => :rights
end
You can get all of a users customers using associations and some Ruby:
# User
def customers
self.groups.collect {|group| group.customers}.flatten.uniq
end
Or you can use some SQL:
# User
def customers
Customer.find(:all, :readonly => false,
:joins => "INNER JOIN rights ON customers.id = rights.customer_id
INNER JOIN memberships ON rights.group_id = memberships.group_id"
:conditions => ["memberships.user_id = ?", self]
end
--
Josh Susser
http://blog.hasmanythrough.com
--
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
-~----------~----~----~----~------~----~------~--~---