嘿铁路人员,确实知道ActiveScaffold的任何等价物但是对ORM不可知或者至少对于Mongoid?
我正在使用Mongoid,它位于Ruby MongDB驱动程序之上.即使我的Map的发出给出了一个parseInt(num),而Reduce的回报也给了a parseInt(num),最终的结果仍然是浮点数.
这是MongoDB特有的吗?任何使它成为整数的方法?
我有一个模型:
class City
include Mongoid::Document
field :name
embeds_many :stores
index [["stores.location", Mongoid::GEO2D]]
end
class Store
include Mongoid::Document
field :name
field :location, :type => Array
embedded_in :cities, :inverse_of => :stores
end
Run Code Online (Sandbox Code Playgroud)
然后我试着打电话City.stores.near(@location).
我想查询City集合以返回Store在附近位置至少有1个的所有城市.我该如何设置索引?什么是最快的电话?
我使用Mongoid文档阅读,index [[:location, Mongo::GEO2D]]但我不确定这是如何应用于嵌入式文档,或者如何只获取City而不是所有Stop文档.
当你有一个与模型embeds_many :album_items有关的关系时AlbumItem.我怎样才能将它存储在内items.我试着embeds_many :album_items, :as => :items和embeds_many :items, :class_name => AlbumItem.都没有奏效.
我怎样才能重新命名关系?
谢谢
如何在Mongoid中使用AND条件进行查询?
ruby-on-rails mongodb mongoid ruby-on-rails-3 ruby-on-rails-3.1
我对mongoid和rails很新.所以我有一些麻烦让树结构工作:
我找到了三个"解决方案"来构建
mongoid-tree(这是最实际的) https://github.com/benedikt/mongoid-tree
并且mongoid提供了recursively_embeds_more的解决方案
mongoid_acts_as_tree https://github.com/saks/mongoid_acts_as_tree
我的目标是为音乐风格制作一棵树,可以在不同的模型中引用/嵌入.
我的模型现在看起来像这样:
class Musicstyle
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Tree # mongoid-tree Version
recursively_embeds_many # mongoids version itself
field :musicstyle, type: String
field :description, type: String
field :parent
end
Run Code Online (Sandbox Code Playgroud)
和观点
<div class="field">
<%= f.label :parent %>
<%= f.select :parent,
Musicstyle.all.map { |m| [m.musicstyle, m._id] },
{:include_blank => "Select a parent (if needed)"} %>
</div>
Run Code Online (Sandbox Code Playgroud)
我现在搜索了几个小时的工作示例,没有成功.
也许有人可以为我提供一些代码以便更好地理解
任何帮助都会让我的一天
很多,非常感谢提前
我切换到Mongoid 3,这使得一些不同的东西:)目前我尝试检查复合字段是否是唯一的:
class Host
include Mongoid::Document
field :ip, :type => String
field :port, :type => Integer
field :username, :type => String
field :password, :type => String
validates_presence_of :ip
validates_presence_of :port
end
Run Code Online (Sandbox Code Playgroud)
如何在其中获取validates_uniqueness_of,检查ip和port是否是唯一的复合字段?AFAIK在Mongoid 2中有一种方法可以基于多个字段创建一个新的_id,但似乎在Mongoid 3中删除了它:
key :ip, :port
Run Code Online (Sandbox Code Playgroud) 我有2个Mongoid模型,看起来像这样:
class User
include Mongoid::Document
field :name, type: String
embeds_many :jobs
end
class Job
include Mongoid::Document
field :title, type: String
embedded_in :user
end
Run Code Online (Sandbox Code Playgroud)
This allows me to do something like
user.jobs.create(title: 'Test Job')
Run Code Online (Sandbox Code Playgroud)
However, I'd like to be able to have some predefined jobs for a user to choose from, which would then be embedded in the user's document. Something like this:
Job.create(title: 'Predefined Job')
user.jobs << Job.first
Run Code Online (Sandbox Code Playgroud)
However, creating a job on it's own throws the following error
Cannot …
我试图限制从mongoid查询返回的不同结果的数量.
Place.where({:tags.in => ["food"]}).distinct(:name).limit(2)
但这会引发以下错误:
NoMethodError: undefined method 'limit' for ["p1", "p2", "p3", "p4"]:Array
如何在mongoid查询中设置限制,而不是获取整个结果集,然后从数组中选择有限数量的项目.
谢谢
我有以下型号
class Professional
include Mongoid::Document
field :first_name, type: String
field :last_name, type: String
field :company_name, type: String
field :address, type: String
validates :first_name, length: { minimum: 5, :message => "What" }, format: { with: /\A[a-zA-Z]+\z/, message: "only allows letters" }
end
Run Code Online (Sandbox Code Playgroud)
我想要包含一个嵌入式文档,我可以存储多个办公地址.我正在寻找以下DB的结构
{
"first_name": "Harsha",
"last_name": "MV",
"company_name": "Mink7",
"offices": [
{
"name": "Head Office",
"address": "some address here"
},
{
"name": "Off Site Office",
"address": "some large address here"
}
]
}
Run Code Online (Sandbox Code Playgroud)