문제

Google is seriously failing me right now. All I need to do is update one attribute, setting a user to admin, from the Heroku rails console.

I can't find a single simple answer. What I've been trying is:

Record.update_attribute(:roles_mask, "1")

Where record is the correct record.

'undefined method 'update attribute''

I can't just type Record.roles_mask = 1?

EDIT.

I used Record, and I shouldn't have done that in the example. What I've done is exactly this:

ian = User.where(:id => '5')

ian.update_attribute(:roles_mask, '1')

Error: undefined method 'update_attributes'
도움이 되었습니까?

해결책

The problem is that using the .where function creates a relation, rather than finding the record itself. Do this:

ian = User.find(5)
ian.update_attribute(:roles_mask, '1')

Or if you want to use .where then you could do this:

ian = User.where(:id => 5)
ian.first.update_attribute(:roles_mask, '1')

EDIT

See this answer for details about why this is happening.

다른 팁

To use update_attribute (or update_attributes), you need to call that from an instance and not the class.

rails c> rec = Record.find(1)
rails c> rec.update_attribute(:att, 'value')
rails c> rec.update_attributes(att: 'value', att2: 'value2')

I would think that should take care of your issue.

Where clause return a array and you are trying to make update query on array, that's why you got error.

you should try to find out first record

ian = User.where(:id => 1).first
or
ian = User.find(1)
or
ian = User.find_by_id(1)

now your update query will work.

ian.update_attribute(:roles_mask, '1')
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top