views:

38

answers:

1

Given these params:

ex: 1

{:field => 'admin', :id => "1"}

ex: 2

{:field => 'client', :id => "1"}

ex: 3

{:field => 'partner', :id => "1"}

Is it possible, (and how of course) could i dynamically apply this to the User model such as:

controller

#note the field attribute is not a field
#the field attribute I'm trying to set above to what are given in the params hash

def update_user
    field = params[:field]
    @user = User.find(params[:id])
    @user.field = [email protected]
    @user.save(false)
    render :nothing => true
end

fyi

Why not just send a has with params and update the with update_attributes(params[:user])? Because the users attributes are protected and it's just easier to update a boolean this way.

+1  A: 

I would do something like this:

@user = User.where("#{params[:field]} = ?", params[:id]).first if ["admin", "client", "partner"].include?(params[:field])

(The "include?" is a security check to prevent users from choosing some other field you don't want them to)

Tim
That's interesting. Not exactly what I was going for but opens other doors. BTW, I'm trying to set based on a dynamic attribute not find
Sam