简体   繁体   English

NoMethodError-Ruby中AWS-SDK中User:Class的未定义方法'where'

[英]NoMethodError - undefined method 'where' for User:Class in aws-sdk for ruby

I am new to DynamoDB. 我是DynamoDB的新手。 I am using aws-sdk for ruby as ORM and I the following error when I try to execute User.where('name'=>'Joe') or User.all(:where => {:name => 'Joe'}) . 我使用aws-sdk作为ORM来处理红宝石,当我尝试执行User.where('name'=>'Joe')User.all(:where => {:name => 'Joe'}) Is this supported with aws-sdk or is there any other way to achieve this. aws-sdk是否支持此功能,或者是否有其他方法可以实现此功能? My model looks like 我的模特看起来像

class User < AWS::Record::HashModel
  string_attr :name
end

Thanks. 谢谢。


Error log: 错误日志:

Started GET "/users" for 127.0.0.1 at 2014-04-10 09:46:39 -0400 Processing by UsersController#index as HTML Completed 500 Internal Server Error in 3ms 在2014-04-10 09:46:39 -0400 HTML中以12ms.0.1的GET“ / users”身份启动UsersController#index处理HTML,在3毫秒内完成500内部服务器错误

NoMethodError (undefined method 'where' for User:Class): app/controllers/users_controller.rb:6:in index' NoMethodError( 'where' for User:Class): app/controllers/users_controller.rb:6:in未定义方法'where' for User:Class): app/controllers/users_controller.rb:6:in index'

It seems HashModel is incomplete and where is only supported by AWS::Record::Model (SimpleDB). HashModel似乎不完整,仅在AWS :: Record :: Model(SimpleDB)支持的地方。

The easiest way around I found is to fallback to the dynamo_db_table object. 我发现的最简单方法是回退到dynamo_db_table对象。

For arbitrary parameters, using SCAN: 对于任意参数,请使用SCAN:

User.dynamo_db_table.items.where(name: 'Joe').select('id', 'surname').map(&:attributes)
User.dynamo_db_table.items.where(name: 'Joe', birthday: ('2014-04-10'..'2014-04-17')).select(*User.attributes.keys).map(&:attributes)
User.dynamo_db_table.items.where(name: 'Joe', birthday: ('2014-04-10'..'2014-04-17')).select(*User.attributes.keys, limit: 1).map(&:attributes)

For querying by hash_key, using QUERY: 对于使用hash_key进行查询,请使用QUERY:

User.dynamo_db_table.items.query(hash_value: 'Joe', select: User.attributes.keys).map(&:attributes)
User.dynamo_db_table.items.query(hash_value: 'Joe', select: User.attributes.keys, limit: 1).map(&:attributes)
User.dynamo_db_table.items.query(hash_value: 'Joe', select: User.attributes.keys, limit: 1, scan_index_forward: false).map(&:attributes)
User.dynamo_db_table.items.query(hash_value: 'Joe', range_value: '2014-04-10'..'2014-04-17', select: [:birthday], limit: 1, scan_index_forward: false).map(&:attributes)

More information here: http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/DynamoDB/ItemCollection.html#query-instance_method 此处的更多信息: http : //docs.aws.amazon.com/AWSRubySDK/latest/AWS/DynamoDB/ItemCollection.html#query-instance_method

As per the error, User class doesn't have a class method named where present in it or in any of its ancestors. 根据错误, User类没有一个在其或其任何祖先where存在的where命名的类方法。

Instead use User.all(:where => { :name => 'Joe' }) . 而是使用User.all(:where => { :name => 'Joe' })

Refer to the Documentation of AWS::Record::HashModel Class 请参阅AWS :: Record :: HashModel类的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM