# Authorization recipe help

**URL:** <https://www.ruby-forum.com/t/authorization-recipe-help/129498>\
**Category:** Rails\
**Created:** [February 19, 2008, 3:42am UTC](https://www.ruby-forum.com/t/authorization-recipe-help/129498 "2008-02-19T03:42:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jvazquez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/858c86/32.png) [@jvazquez](https://www.ruby-forum.com/u/jvazquez)\
**Post date:** [February 19, 2008, 3:42am UTC](https://www.ruby-forum.com/t/authorization-recipe-help/129498/1 "2008-02-19T03:42:02Z")

</div>

Hello all,  
I am following the rails recipe book for authorization and it works  
as written. I have created a controller that will be administrate  
accounts and want to show each user with all their roles. I can’t seem  
to get the roles to match up. This is my logic

1. query RolesUsers (passing in the userid)

2 get the name of the role from Roles passing in the role\_id received  
from step one

my problem is that I see this when I puts the result of step one

#\<RolesUsers role\_id: 1, user\_id: 1\>

I am still new to ruby and a little confused as to what I am seeing.  
it appears to be a class object, but from what I am use to seeing, it  
is missing the @ in front of role\_id and user\_id.

I get an error if I do this, privs.role\_id

any clue what is happening?  
Thanks  
-Juan

---

<div class="post-metadata">

**Author:** ![jvazquez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/858c86/32.png) [@jvazquez](https://www.ruby-forum.com/u/jvazquez)\
**Post date:** [February 19, 2008, 3:55am UTC](https://www.ruby-forum.com/t/authorization-recipe-help/129498/2 "2008-02-19T03:55:55Z")

</div>

Hi,

Lets step from here :

privs= RolesUsers.get\_roles(userid)

what your “get\_roles(userid)” is going to RolesUsers Active Record or  
Model? if yes, can you paste here your def get\_roles(any), so we can  
understand well your problem.

_~_~_~_~_~\*\*~_~_~_~_~_  
Reinhart Ariando  
YM : Booking2Heaven  
WEB : [http://teapoci.blogspot.com](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![jvazquez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/858c86/32.png) [@jvazquez](https://www.ruby-forum.com/u/jvazquez)\
**Post date:** [February 19, 2008, 4:06am UTC](https://www.ruby-forum.com/t/authorization-recipe-help/129498/3 "2008-02-19T04:06:39Z")

</div>

This is what I have, if there is a better way, I would be happy to see  
it. Thanks

class RolesUsers \< ActiveRecord::Base  
def self.get\_roles(userid)  
find(:all, :conditions =\> [“user\_id = ?”, userid])  
end

end

On Feb 18, 8:55 pm, Visit Indonesia 2008 \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![jvazquez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/858c86/32.png) [@jvazquez](https://www.ruby-forum.com/u/jvazquez)\
**Post date:** [February 19, 2008, 5:12am UTC](https://www.ruby-forum.com/t/authorization-recipe-help/129498/4 "2008-02-19T05:12:24Z")

</div>

it makes Perfect sense now!! thank you!

On Feb 18, 9:27 pm, Visit Indonesia 2008 \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![jvazquez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/858c86/32.png) [@jvazquez](https://www.ruby-forum.com/u/jvazquez)\
**Post date:** [February 19, 2008, 4:27am UTC](https://www.ruby-forum.com/t/authorization-recipe-help/129498/5 "2008-02-19T04:27:35Z")

</div>

Hello,

> class RolesUsers \< ActiveRecord::Base  
> def self.get\_roles(userid)  
> find(:all, :conditions =\> [“user\_id = ?”, userid])  
> end

You use find :all, so that the result of your searching will be formed  
as array. you can not run privs.role\_id. First let we count how many  
array is there using :

@size = privs.size

example you got 1

you execute like this privs[0].role\_id or to see all list of role\_id you  
can use like it:

for kungfu in privs  
print kungfu.role\_id  
end

remember the ways below looks like the same but having difference result  
:

[First] @fighter = WorldFigther.find(1)

and

[Second] @fighter = WorldFigther.find(:all, :conditions =\> [“id = ?”,  
1])

To get fighter name of the First is @fighter.name, but the second is  
@fighter[0].name

I hope you can understand clearly, your problem is.

_~_~_~_~_~\*\*~_~_~_~_~_  
Reinhart Ariando  
YM : Booking2Heaven  
WEB : [http://teapoci.blogspot.com](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![jvazquez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/858c86/32.png) [@jvazquez](https://www.ruby-forum.com/u/jvazquez)\
**Post date:** [February 19, 2008, 4:31pm UTC](https://www.ruby-forum.com/t/authorization-recipe-help/129498/6 "2008-02-19T16:31:54Z")

</div>

@Juan,

You’re working your way into the infamous N+1 db problem… I believe  
that the recipe has a User model like this:

class User  
has\_and\_belongs\_to\_may :roles  
…  
end

To collect all the roles, you should rely on the association that you  
have defined:

@current\_user = … (User.find(session[:userid] iirc)  
@roles = @current\_user.roles

@roles.each do |role|  
puts role.name  
end

---

<div class="post-metadata">

**Author:** ![jvazquez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/858c86/32.png) [@jvazquez](https://www.ruby-forum.com/u/jvazquez)\
**Post date:** [February 20, 2008, 5:15am UTC](https://www.ruby-forum.com/t/authorization-recipe-help/129498/7 "2008-02-20T05:15:09Z")

</div>

this is fantastic, could you explain what you mean by the infamous N+1  
db problem? I will be the first to admit I am not well versed in db  
lingo, is this a performance measure? I would like to learn as much as  
possible from this mistake, so that I don’t repeat it in the future.  
thanks again for the help.  
-Juan

---

<div class="post-metadata">

**Author:** ![jvazquez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/858c86/32.png) [@jvazquez](https://www.ruby-forum.com/u/jvazquez)\
**Post date:** [February 20, 2008, 9:42am UTC](https://www.ruby-forum.com/t/authorization-recipe-help/129498/8 "2008-02-20T09:42:28Z")

</div>

What is the meaning of infamous N+1 db problem? sorry i am not good in  
db analyst and design.

---

<div class="post-metadata">

**Author:** ![jvazquez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/858c86/32.png) [@jvazquez](https://www.ruby-forum.com/u/jvazquez)\
**Post date:** [February 20, 2008, 8:26pm UTC](https://www.ruby-forum.com/t/authorization-recipe-help/129498/9 "2008-02-20T20:26:54Z")

</div>

N+1…

Assume the user has N roles. If you grab the user it will take 1 db  
round trip followed by N more to retrieve each of the roles for a  
total of N+1 round trips. db access is likely to be one of the most  
costly aspects of your application so you want to eliminate that as  
much as possible.

Enter the has\_many :through =\> xxx and has\_and\_belongs\_to\_many (the  
latter is out of favor since the former permits you a full model that  
may contain extra state information about the join – like a  
subscription class that joins a customer to his periodical but also  
tells you whether the customer is paid up). If/when you have one of  
these specified you can access the associated models through the named  
association and Rails will automatically do a join for you. So…

class User  
has\_many :user\_roles  
has\_many :roles, :through=\>:user\_roles  
…  
end

user = User.find(…, :include=\>:roles)

The above find will trigger a left outer join so that you collect the  
user and ALL of his associated roles in ONE db roundtrip.  
On Feb 20, 3:42 am, Visit Indonesia 2008 \<rails-mailing-l…@andreas-
