关系类有属性错误
为什么关系类属性不是属性?
$ 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>
模型:
class ResourceServer < ActiveRecord::Base
belongs_to :server
belongs_to :resource
# Validations
...
end
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这只是因为您在类
ResourceServer
上调用#attributes=
实例方法,而不是在对象rs
上调用。你想做的是:
它会起作用! :)
It is just because your are calling the
#attributes=
instance method on the classResourceServer
and not on the objectrs
.What you want to do is:
And it will work! :)
ResourceServer 是一个类,您需要该类的实例才能为其分配属性。例如你可以这样做:
ResourceServer is a class, you need an instance of that class in order to assign attributes to it. For example you can do: