开发者

Relationship clasess has attribute error

Why relationship classes attribute is not attribute?

$ rs = ResourceServer.new
 => #<ResourceServer id: nil, resource_id: nil, server_id: nil, created_at: nil, updated_at: nil> 

$ rs = ResourceServer.attributes = {:server_id => 1, :resource_id => 1}
 NoMethodError: undefined method `attributes=' for #<Class:0x00000003384728>
开发者_如何转开发

Model:

class ResourceServer < ActiveRecord::Base
  belongs_to :server
  belongs_to :resource

  # Validations
...
end


It is just because your are calling the #attributes= instance method on the class ResourceServer and not on the object rs.

What you want to do is:

rs.attributes = {:server_id => 1, :resource_id => 1}

And it will work! :)


ResourceServer is a class, you need an instance of that class in order to assign attributes to it. For example you can do:

rs = ResourceServer.new
rs.attributes = {:server_id => 1, :resource_id => 1}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜