Joe Essey
Joe Essey

Reputation: 3527

Setting param attribute explicitly, not updating with update_attributes call

My controller calls this model method on update:

def update_standard(param_attributes)

...

if param_attributes[:is_legacy] == true
  param_attributes[:foo_type_id] = 2
end

  update_attributes(param_attributes)

end

foo_type_id should overwrite whatever the user entered in the form, but the user's choice is what is written to the DB. How can I enforce foo_type_id being 2 when is_legacy is true?

Upvotes: 0

Views: 83

Answers (3)

Kishore Mohan
Kishore Mohan

Reputation: 1060

param_attributes[:is_legacy] == "true" ? param_attributes[:foo_type_id] = 2 : param_attributes[:foo_type_id]
update_attributes(param_attributes)

Upvotes: 2

Veraticus
Veraticus

Reputation: 16064

You should check to make sure that params[:is_legacy] actually returns true and not "true" -- pretty sure params are always strings.

Upvotes: 1

SomeDudeSomewhere
SomeDudeSomewhere

Reputation: 3940

I think there could be some issues with your logic evaluation.

Try changing this to the following and see if it works?

if !param_attributes[:is_legacy].nil? && param_attributes[:is_legacy].to_sym == :true
  param_attributes[:foo_type_id] = 2
end

Upvotes: 0

Related Questions