Better off saying !roles.find_by_name("admin").blank?
Saves on SQL

Blog: http://random8.zenunit.com/
Twitter: http://twitter.com/random8r
Learn: http://sensei.zenunit.com/
New video up now at http://sensei.zenunit.com/ real fastcgi rails deploy process! Check it out now!


On 07/04/2010, at 11:40 PM, Sharagoz <[email protected]> wrote:

user1.roles returns an array of roles.
The array class has a function "include?(obj)" that returns true or
false.

Something like this should work:
user1.roles.include?(Role.find_by_name('admin'))

Named scopes are not ment for stuff like this.
You can create a function on the user model "is_admin?" for the code
above. That way if how an admin is defined changes, you only need to
update the code in one place.

On Apr 7, 1:38 pm, Tom Mac <[email protected]> wrote:
Hi
I have the models roles, users and user_roles. The relationship among
them is

user.r
-------
user has_many user_roles
user has_many roles, :through => :user_roles

role.rb
=======

role has_many user_roles
role has_many users, :through => :user_roles

user_role.rb
========
belongs_to :user
belongs_to :role

Suppose a login user1 has roles [admin, staff] And login user2 has
only one role say [participant] Now how can I check the cases

1)a login user is an admin or staff
2) a login user has_role participant?

      And also I would like to know whether this can be written using
named scopes.(I am totally new to named scopes)

Thanks in advance
Tom
--
Posted viahttp://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- [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 .


--
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