>
>     user_statuses is a master table having values
>
> id name
> 1  Pending
> 2  Approved
> 3  Deleted
>

My first question is why do you have a table for user_status?  Are
administrators of your app going to be adding new statuses via a web
interface?

if not, why not just have constants:

class User
  STATUS_PENDING = 1
  STATUS_APPROVED = 2
  STATUS_DELETE = 3

  ...
end


> user = User.find(params[:id])
> user.update_attributes(:user_status_id => 2)
>
>      Rather than hard coding user_status_id = 2,  is there any other
> approach?
>

user.update_attributes(user_status => User::STATUS_APPROVED)

It sounds like you're making it more complex than it needs to be.

Alternatively if you wanted to keep it your way you could do:

user.update_attributes(:user_status_id =>
UserStatus.find_by_name("Approved").try(:id))

This finds the UserStatus record with the name Approved and uses it's id (if
it's found)

Cheers,


Andy
--
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-t...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to