开发者

Why is my update failing in a each loop?

开发者 https://www.devze.com 2023-01-25 14:38 出处:网络
users = User.all() user.each do |u| b = get_id_blah() u.some_id = b.id u.save end I get the error: ruby-1.8.7-p302@rails3/gems/activemodel-3.0.1/lib/active_model/attribute_methods.rb:364:in `me
users = User.all()

user.each do |u|

  b = get_id_blah()

  u.some_id = b.id
  u.save

end

I get the error:

ruby-1.8.7-p302@rails3/gems/activemodel-3.0.1/lib/active_model/attribute_methods.rb:364:in `method_missing': private method `update' called for #<User:0x1017b8188> (NoMethodError)

Should I be calling save outside of the l开发者_如何学Pythonoop?


This might just be a typo, but it could explain your no-method-error

users = User.all()

user**s**.each do |u|


end

It should be something like

users = User.all
users.each do |user|
  ....
end

Or simply

User.all.each do |user|
   ...
end


Are you sure you this is the exact code? It sounds like you are calling object.update instead of just calling object.save

By the way, you don't need parentheses if you're calling functions in Ruby :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消