这是我的模型中的代码
include Mongoid::Document
include Mongoid::Timestamps
field :message, :type => String
field :send_at, :type => DateTime
Run Code Online (Sandbox Code Playgroud)
这是我的表单部分的代码
<%= f.label :send_at %><br />
<%= f.datetime_select :send_at %>
Run Code Online (Sandbox Code Playgroud)
但是从不填充日期和时间.我确保Mongo和Mongoid也是最新的.不确定是否有我遗漏的东西.
[更新日志条目]
Started POST "/notifis" for 127.0.0.1 at Mon Oct 18 05:48:05 -0400 2010
Processing by NotifisController#create as HTML
Parameters: {"commit"=>"Create Notifi",
"authenticity_token"=>"/hrlnvA2Xn5NqGgCkPFAQV254IHPJEvZoLxOYNNUwhc=", "_snowman"=>"?",
"notifi"=>{"send_at(2i)"=>"10", "is_sent"=>"0", "send_at(3i)"=>"18",
"send_at(4i)"=>"09", "message"=>"erwer", "send_at(5i)"=>"48",
"send_at(1i)"=>"2010"}}
MONGODB noti_development['notifis'].insert([{"send_at(2i)"=>"10", "created_at"=>Mon Oct
18 09:48:05 UTC 2010, "is_sent"=>false, "updated_at"=>Mon Oct 18 09:48:05 UTC 2010,
"_id"=>BSON::ObjectID('4cbc17d5c24d7602bc00002d'), "send_at(3i)"=>"18",
"message"=>"Sample Message", "send_at(4i)"=>"09", "send_at(1i)"=>"2010",
"send_at(5i)"=>"48"}])
Redirected to …Run Code Online (Sandbox Code Playgroud) 我有一个定义如下的联系信息类:
class ContactInfo
include Mongoid::Document
validates_presence_of :name, :message => ' cannot be blank'
field :name, :type => String
field :address, :type => String
field :city, :type => String
field :state, :type => String
field :zip, :type => String
field :country, :type => String
embedded_in :user
end
Run Code Online (Sandbox Code Playgroud)
此联系信息类嵌入在我的用户类中的嵌套属性:
class PortalUser
include Mongoid::Document
accepts_nested_attributes_for :contact_info
end
Run Code Online (Sandbox Code Playgroud)
当我尝试保存没有名称的用户时,我收到如下错误消息:
联系信息无效
但是,这对最终用户来说不是很有用,因为他或她不知道哪些联系信息无效.REAL消息应为"名称不能为空".但是,此错误不会向上传播.有没有办法让user.errors中的'Name not not blank'消息而不是'Contact info is invalid'错误消息?
谢谢
我似乎找不到使用Mongoid/Rails进行日期范围查询的任何内容.以下是我尝试的一些查询(大约100个其他查询).如果它返回任何内容,它总是忽略'end_date'.两个日期都是日期时间......
all(:conditions => {:created_at => start_date.to_datetime..end_date.to_datetime})
Run Code Online (Sandbox Code Playgroud)
结果是:
NoMethodError: undefined method `to_i' for Tue, 26 Apr 2011 00:00:00 +0000..Fri, 06 May 2011 00:00:00 +0000:Range
Run Code Online (Sandbox Code Playgroud)
另一个例子...
where(:created_at => {'$gte' => start_date,'$lt' => end_date})
Run Code Online (Sandbox Code Playgroud)
导致查询成功,但忽略结束日期.结果在开始日期之后正确返回,但不受结束日期的限制.
几天来我一直在研究这个问题但无济于事.在论坛或IRC网上帮助不大.
很想知道我做错了什么:)
我正在使用Mongoid ruby gem来与MongoDB进行交互,当我尝试从查询中获取某个东西时,它会添加$limit: -1(即负数)当我希望它只是使用时1.我尝试在控制台中执行相同的操作,但它没有更改返回的文档.
负面限制意味着什么特别的东西吗?
MongoID 文档似乎很清楚,我应该能够运行它并让它工作:
Band.find_by(name: "Photek")
Run Code Online (Sandbox Code Playgroud)
但至少在MongoID 2.4.11这给了我一个NoMethodError.
另一方面,这有效:
Band.find(name: "Photek")
Run Code Online (Sandbox Code Playgroud)
这是很容易改变find_by到find,但我很困惑这是怎么回事.这是我的宝石版本落后于文档的情况,还是什么?
我在mongoDB中找到了一些完成此功能的信息,但我需要mongoid.所以我可以这样做:
User.last(7000).each do ....
我正在使用:
MongoDB shell版本:2.4.3
Mongoid 2.6.0
谢谢!
我在Rails项目中使用Mongoid(均为4.0.x),并且我有一个带有哈希字段的文档,该字段存储一些无模式数据.
class Thing
field :name, type: String
field :mass, type: Integer
field :info, type: Hash
end
Run Code Online (Sandbox Code Playgroud)
通过这种设置,我可以查询具有如下关键字的内容:endDate:
Thing.where("info.endDate"=>{'$exists'=>true})
Run Code Online (Sandbox Code Playgroud)
这一切都很好用.对这个:info字段使用哈希字段是很好的,因为我想要存储的东西没有固定的模式,并且从一个东西到另一个东西各不相同.
好吧,但是,我不能$set对:info哈希中的键/值对使用相同的点语法.[1]
thing.set("info.endDate"=>Time.now)
Run Code Online (Sandbox Code Playgroud)
引发Mongoid::Errors::UnknownAttribute错误.
它告诉我,我必须Mongoid::Attributes::Dynamic在我的模型中包含这样做,但这对我来说似乎不对.哈希字段类型的要点似乎是允许您处理没有固定模式的数据.我似乎不应该包含一个特殊的"动态属性"模块来使用哈希字段.
所以现在,我正在使用常规的旧[]语法更新值,然后调用save模型,如下所示:
thing.info[:endDate] = Time.now
thing.save
Run Code Online (Sandbox Code Playgroud)
但是很多时候它会发生这样$set的价值更好.是否有其他语法来设置哈希字段值?我错误的上述错误消息和动态属性是错误的?我暂时不想对哈希字段进行两步更新吗?
[1]不可否认,我最近从mongomapper迁移过来,所以我对这种语法的期望部分是由于能够在mongomapper中做到这一点.
我在MongoDB Collection中有几个文件,带有一个字段'name'(这是一个String).
我该如何执行像这样的查询 7 <= name.length <= 14
为什么is_a?返回false一Hash类?
例:
value = {"x" => 3, "y" => 2}
puts value.class
puts value.is_a?(Hash)
Run Code Online (Sandbox Code Playgroud)
输出:
Hash
false
Run Code Online (Sandbox Code Playgroud)
我使用Ruby 1.9.2
更新:我的课程的完整来源:
class LatLng
include Mongoid::Fields::Serializable
attr_reader :lat, :lng
def serialize(value)
return if value.nil?
puts value.class
puts value.is_a?(Hash)
if value.is_a?(self.class)
puts "is geopoint" + value.to_json
{'lng' => value.lng.to_f, 'lat' => value.lat.to_f}
elsif value.is_a?(Hash)
hash = value.with_indifferent_access
puts "is hash" + value.to_json
{'lng' => hash['lng'].to_f, 'lat' => hash['lat'].to_f}
end
end
def deserialize(value)
return if value.nil?
value.is_a?(self.class) …Run Code Online (Sandbox Code Playgroud) 我刚开始一个新的rails项目,想通过Mongoid gem使用MongoidDB.按照Mongoid网站上的说明,我向我添加了以下行Gemfile:
gem "mongoid", "~> 2.4"
gem "bson_ext", "~> 1.5"
Run Code Online (Sandbox Code Playgroud)
然后我database.yml按照此处的说明继续删除我的文件.我的application.rb文件现在看起来像这样:
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
require "sprockets/railtie" # Uncomment this line for Rails 3.1+
Run Code Online (Sandbox Code Playgroud)
现在,当我用于rails s在开发中启动我的服务器时,我收到以下错误:
~/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.0/lib/rails/railtie/configuration.rb:85:in `method_missing': undefined method `active_record' for #<Rails::Application::Configuration:0x007ff38b20d0b0> (NoMethodError)
Run Code Online (Sandbox Code Playgroud)
我试着找一个解决方案,但似乎还没有人遇到我的问题.难道我做错了什么?这是由最近的Rails 3.2更新引起的吗?
谢谢你的帮助!
更新(1月26日): 根据Dylan Markow的信息,我使用了terminal命令
grep -r active_record config/
Run Code Online (Sandbox Code Playgroud)
并在评论块中将任何引用放入active_record.
我有一个简单的控制器,其中一个动作甚至还没有打到数据库.当我通过浏览器访问操作时,我得到了
ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished):
activerecord (3.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `retrieve_connection'
activerecord (3.2.0) lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in `retrieve_connection'
activerecord (3.2.0) lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in `connection'
activerecord (3.2.0) lib/active_record/query_cache.rb:67:in `rescue in call' …Run Code Online (Sandbox Code Playgroud)