我在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中做到这一点.
我有一个多域Rails 4应用程序,其中request.domainhttp请求确定我公开给定访问者的功能.
我的应用中的每个域都应该由自己的MongoDB数据库提供服务.例如domain1.com由db_for_domain_1等提供.
我可以在MongoDB文档中阅读有关运行时持久性的内容
Mongoid.override_database("db_for_#{request.domain}")
Run Code Online (Sandbox Code Playgroud)
使我能够动态切换数据库.
但是当我绕过Mongoid并使用mongo Shell方法db.collection.insert()时,如何保持持久性呢?我仍然会在我的应用程序中执行此操作.
答案可能在关于集合访问的MongoDB文档中,但我不明白.那么如何在此操作之前/期间切换数据库?:
MyModel.collection.insert({field_1: "Value 1", field_2: "Value 2"})
Run Code Online (Sandbox Code Playgroud) 我有以下模型结构:a Banner embeds_many Slides和eachSlide embeds_many contents
class Banner
include Mongoid::Document
embeds_many :slides
accepts_nested_attributes_for :slides, allow_destroy: true
end
class Slide
include Mongoid::Document
embedded_in :banner
embeds_many :contents
accepts_nested_attributes_for :contents, allow_destroy: true
end
class Content
include Mongoid::Document
embedded_in :slide
field :value, type: String
end
Run Code Online (Sandbox Code Playgroud)
我开始使用一张带有一些内容的幻灯片的横幅.现在我向服务器发送一个JSON请求,将新内容添加到现有幻灯片中,并创建包含其内容的新幻灯片; 就像是
'banner' => {
'_id' => '123',
'slides_attributes' => [
{
'_id' => '1',
'contents_attributes' => [
{ '_id' => '1', 'value' => 'some persisted value' },
{ 'value' => 'new content here, there is no …Run Code Online (Sandbox Code Playgroud) single-table-inheritance nested-attributes mongoid4 ruby-on-rails-4.1
我有一个带有哈希字段的Mongoid模型.此Mongoid模型具有使用单个集合继承的子类.现在我想为主模型的每个子类设置不同的默认哈希键.
主要模型
class Sport
include Mongoid::Document
field :rules, type: Hash, default: {}
end
Run Code Online (Sandbox Code Playgroud)
我想为:rule hash字段设置不同的默认哈希键的子类.例如,对于足球我想要规则:{:offside =>'',:penalty =>''}对于拳击我们可能有规则的哈希键:{:biting =>'not allowed'}.开源应用程序Errbit使用子类中的常量来设置默认哈希键,但我可以看到他们如何使用常量来填充哈希:https://github.com/errbit/errbit/blob/master/app/车型/ issue_trackers/github_issues_tracker.rb
class Sport::Football < Sport
end
class Sport::Boxing < Sport
end
Run Code Online (Sandbox Code Playgroud)
我确实覆盖了子类中的字段定义,如下所示,它在rails控制台中有效.当我做一个= Sport :: Football.new然后调用a.smtp返回默认设置.但问题是,当我去父类并执行b = Sport.new和b.smtp时,我希望它能够列出子类的所有默认键,但它没有,因为我已经覆盖了子类中的哈希字段.
class Sport::Football < Sport
field :rules, type: Hash, default: {:offside => '', :penalty => ''}
end
Run Code Online (Sandbox Code Playgroud)
有没有办法为子类设置默认哈希键而不覆盖字段定义.可以通过覆盖每个子类中哈希字段的setter和getter来做到这一点.
我在Mongo中有一个带有两个数组字段的文档:
field :app_usernames, type: Array
field :email_addresses, type: Array
Run Code Online (Sandbox Code Playgroud)
我想创建一个函数,它将一组用户名和一组电子邮件地址用于搜索集合.踢球者是我希望它返回具有数组中传递的任何值的文档:
def find_people(usernames_to_search, emails_to_search)...
Run Code Online (Sandbox Code Playgroud)
所以给出一个带字段值的文档:
app_usernames = ['test1','test2','test3']
email_addresses = ['test1@test.com','test2@test.com']
Run Code Online (Sandbox Code Playgroud)
我希望函数在通过数组参数搜索任何值时找到它.它应该在以下情况下返回此文档:
find_people nil,['test1@test.com']
find_people ['test3'],['test1@test.com']
find_people ['oldusername'],['test1@test.com']
Run Code Online (Sandbox Code Playgroud)
最后一个似乎给我带来了麻烦.
到目前为止,我已经尝试过
.or(:app_usernames.in usernames_to_search, :email_addresses.in emails_to_search)
Run Code Online (Sandbox Code Playgroud)
但无济于事.
如何跳过mongoid中关系的默认范围?
可转移的关注点在模型上实现了软删除,并添加了以下内容
field :d_at, type: DateTime
default_scope -> { where(d_at: nil) }
Run Code Online (Sandbox Code Playgroud)
如果一个品牌遭到破坏,我仍然希望在我加载一个引用该品牌的产品时可以使用它们这些是模型定义
class Product
include Mongoid::Document
field :title, type: String
belongs_to :brand, class_name: 'Brand'
end
class Brand
include Mongoid::Document
include Concerns::Trashable
field :title, type: String
end
Run Code Online (Sandbox Code Playgroud)
例:
product = Product.find([id])
puts product.brand.inspect #This brand is soft-deleted and not fetched because of the default scope
Run Code Online (Sandbox Code Playgroud)
这样可行,但它会解决更多问题
class Product
include Mongoid::Document
field :title, type: String
belongs_to :brand, class_name: 'Brand'
#unscope relation to brand
def brand
Brand.unscoped.find(self.brand_id)
end
end
Run Code Online (Sandbox Code Playgroud) ReleaseSchedule.next_release我在其他控制器中调用
并得到以下错误
NoMethodError (undefined method `to_criteria' for #<ReleaseSchedule:0x007f9cfafbfe70>):
app/controllers/weekly_query_controller.rb:15:in `next_release'
Run Code Online (Sandbox Code Playgroud)
class ReleaseSchedule
scope :next_release, ->(){ ReleaseSchedule.where(:release_date.gte => Time.now).without(:_id, :created_at, :updated_at).first }
end
Run Code Online (Sandbox Code Playgroud) 如果我有几个ID,请说 [1,2,3]
是否可以在Mongoid中一次查询所有内容?如:
User.where({ id: [1,2,3]}) 或类似的东西?
我正在尝试使用MongoDB(mongoid)和MySQL一起在rails 4中创建应用程序.但是我无法设置它.
我按照以下步骤操作:
rails new myapp -d mysqlgem "mongoid"gem "bson_ext"bundle installrails g mongoid:config第4步失败.我无法弄清楚问题所在.它显示以下错误.
/home/devesh/.rvm/gems/ruby-2.2.2/gems/mongoid-1.0.6/lib/mongoid/associations.rb:5:in
require': /home/devesh/.rvm/gems/ruby-2.2.2/gems/mongoid-1.0.6/lib/mongoid/associations/has_many.rb:79: syntax error, unexpected keyword_do_cond, expecting ':' @documents = attributes ? attributes.collect do |attrs| ^ /home/devesh/.rvm/gems/ruby-2.2.2/gems/mongoid-1.0.6/lib/mongoid/associations/has_many.rb:84: syntax error, unexpected ':', expecting keyword_end end : [] ^ /home/devesh/.rvm/gems/ruby-2.2.2/gems/mongoid-1.0.6/lib/mongoid/associations/has_many.rb:99: syntax error, unexpected keyword_do_cond, expecting keyword_end attributes.values.each do |attrs| ^ /home/devesh/.rvm/gems/ruby-2.2.2/gems/mongoid-1.0.6/lib/mongoid/associations/has_many.rb:139: syntax error, unexpected keyword_end, expecting end-of-input (SyntaxError) from /home/devesh/.rvm/gems/ruby-2.2.2/gems/mongoid-1.0.6/lib/mongoid/associations.rb:5:in'来自/home/devesh/.rvm/gems/ruby-2.2 .2/gems/mongoid-1.0.6/lib/mongoid.rb:40:require' from /home/devesh/.rvm/gems/ruby-2.2.2/gems/mongoid-1.0.6/lib/mongoid.rb:40:inin'from /home/devesh/.rvm/gems/ruby-2.2.2/gems/bundler-1.10.6/lib/bundler/ runtime.rb:76:在require' from /home/devesh/.rvm/gems/ruby-2.2.2/gems/bundler-1.10.6/lib/bundler/runtime.rb:76:inblock(2级)的require'from …