Reputation: 5735
I get an error trying to set a User model value using send. What is the correct method to use?
SyntaxError ((irb):22: syntax error, unexpected '=', expecting end-of-input)
User.first.send(:first_name)="John"
Upvotes: 0
Views: 779
Reputation: 5735
Figured out a much easier method.
User.first[:first_name] = "John"
Upvotes: 1
Reputation: 5552
Use public_send
when you want to dynamically infer a method name and call it, yet still don't want to have encapsulation issues.
user = User.first
user.public_send(:first_name=, 'Ray')
Perhaps send
will also work in above code but not recommended in most of cases.
User.first.public_send(:first_name=, 'Ray')
will be useless as you are assigning value to User
object and this setter method do not save value.
Your object is not stored in any reference so you will lost object on which you have performed setter
operation for first_name
.
Take it in variable like user & then do such operations so you will have track, so later you can save user object and also you can view what changes you made for that variable
Upvotes: 4