0

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.

5
  • Is update_user supposed to do anything with the local variable field? Commented Oct 18, 2010 at 18:05
  • the point of the field local variable is to dynamically generate the attribute that needs to be updated Commented Oct 18, 2010 at 18:13
  • Are the attributes like admin and client and are they in the db or just instance variables? Commented Oct 18, 2010 at 18:17
  • They are just an example of attributes on a User model. Commented Oct 18, 2010 at 18:34
  • I guess it is really confusing so I will update the code Commented Oct 18, 2010 at 18:34

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.