0

Lots of searching here and around suggested that you could use a second argument to send() to write a value to an attribute, but in Rails 4 you are told you have the wrong number of parameters:

> prj = Project.where(:id => 123).first
> fieldname = "project_start_date"
> prj.send(fieldname, Date.today)
ArgumentError : wrong number of arguments (1 for 0)

That approach was thought to be synonymous with

> prj.write_attribute(fieldname, Date.today)

But that errors with

NoMethodError : private method `write_attribute'

which is odd since the docs say this is a part of the instance public methods.

The ActiveRecord docs suggest using the class update method:

# Updates one record
Person.update(15, user_name: 'Samuel', group: 'expert')

# Updates multiple records
people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
Person.update(people.keys, people.values)
So what's a Rails 4 guy supposed to do?

In my case that would translate to:

Project.update(123, project_start_date: '2013/09/04') #not using variables for testing sake

and that yields me a nice:

 ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR:  zero-length delimited identifier at or near """"
LINE 1: ...dual".* FROM "project"  WHERE "project"."" = $1 LI...

So what's a Rails 4 user supposed to use, other than writing out the actual SQL statements?

1 Answer 1

2

You're close in your first example. The reason send isn't working is because you're essentially trying to do:

prj.project_start_date(Date.today)

which doesn't make sense because the project_start_date method does not take an argument. You need to change it to the setter

prj.project_start_date = Date.today

which would translate to:

prj.send("#{fieldname}=", Date.today)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for uncrossing my eyeballs on this one!

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.