Forum: Ruby on Rails Am I doing it the right way?

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.
Dafe64c50724360a4a2e93ddedbb60ca?d=identicon&s=25 Alan Sikora (Guest)
on 2008-10-10 08:49
(Received via mailing list)
Hello, I'm totally new to RoR but I work with PHP and Java for a
while (7 years)... I'm getting used to Ruby and I'd like to know if
the code below is "right". It is the "show" page of the Users table...

<p>
  <b>Email:</b>
  <%=h @user.email %>
</p>

<p>
  <b>Password:</b>
  <%=h @user.password %>
</p>

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


<%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %>

Thank you!
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-10 08:53
(Received via mailing list)
On Oct 10, 3:56 am, Alan Sikora <alansik...@gmail.com> 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
613c0cc9552a2c880a902bb109c885b0?d=identicon&s=25 Youssef Chaker (Guest)
on 2008-10-11 01:42
(Received via mailing list)
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).
613c0cc9552a2c880a902bb109c885b0?d=identicon&s=25 Youssef Chaker (Guest)
on 2008-10-11 01:42
(Received via mailing list)
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 <
frederick.cheung@gmail.com> wrote:

>
> 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
A45f650cce5746dd89aafb3176b47b02?d=identicon&s=25 DyingToLearn (Guest)
on 2008-10-11 04:42
(Received via mailing list)
Alan Sikora wrote:
>   <b>Email:</b>
...
>   <b>Password:</b>
...
>   <b>Role:</b>


Would probably be better as:
   <strong>Email:</strang>
...
   <strong>Password:</strong>
...
   <strong>Role:</strong>


Long live CSS!
This topic is locked and can not be replied to.