As @Fredrick said, you can do something like
in user.rb
has_many :roles (or has_one :role)
and in role.rb
belongs_to :user

so in your controller you'd call user.role.name (or user.roles.first.name if
you have many roles and want to get the first).

>From what you have I worry if Role.find returns a null object so you'd get
an exception unless you have validations when you create your objects to
make sure every user has a role. To solve this you can add a "unless"
statement: <%=h Role.find(@user.role_id).name unless
Role.find(@user.role_id).nil? %> which means that it won't try to get the
name if it returned a nil so you'd avoid the problem.

On Fri, Oct 10, 2008 at 2:52 AM, Frederick Cheung <
[EMAIL PROTECTED]> wrote:

>
>
>
> On Oct 10, 3:56 am, Alan Sikora <[EMAIL PROTECTED]> wrote:
>
> > <p>
> >   <b>Role:</b>
> >   <%=h Role.find(@user.role_id).name %>              ==========> This
> > line, is it right? Is there a better way to do it?
> > </p>
>
> typically you would have an association between users and roles so
> that you could do user.role
>
> Fred
> >
>


-- 
Youssef Chaker

Software Developer
Open Source Connections

University Of Virginia
Computer Engineering
Class of 2008

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