Another "I'm I right" question...

I'm pretty sure that there's a better way to do this:

@roles = Role.find(:all)
@selected_role = Role.find(@user.role_id)

Can someone help me?

Why not just (assuming user is associated to role):

@selected_role = @user.role

Since this is the second question like this, with essentially the same
answer I'm going to flesh out the answer a little further.

If you have a schema like this:

  t.integer :id
  t.string :name

  t.integer :id
  t.string :first_name
  t.string :last_name
  t.integer :role_id

And models like this:

class Role < ActiveRecord::Base
  has_many :users

class User < ActiveRecord::Base
  belongs_to :role

Then you can do stuff like this:

role = Role.find_by_name("Administrators") # Get the role named
users = role.users # Get all the users in the role
user = role.users.find(:first, :order => :last_name) # Find the first
user in the role ordered by last name # Get the name of the role of the user

Erol Fornoles wrote: