我使用rails和mongodb(mongoid gem).我需要创建一个包含特定元素的选择表单,这些元素嵌入在文档中.该文件如下:
App - > Order - > Package
我想获得包具有特定值的app-documents.任何建议如何实现这一目标?我尝试了以下方法,但不起作用:
@apps = current_user.apps.order.all(conditions: { order.package: 2 } )
Run Code Online (Sandbox Code Playgroud) 我不确定嵌入式类的控制器应该怎么做.传递的参数确实显示了所有嵌入的类属性,包括图像属性,但只有非图像参数才会保存在数据库中.这告诉我问题不在于ORM的选择(在这种情况下是Mongoid),而是与我使用载波的方式有关:
Parameters: {"article"=>{"name"=>"New article", "comments_attributes"=>{"0"=>{"remote_image_url"=>"", "name"=>"Comment 1", "content"=>"comment content....", "image"=>#<ActionDispatch::Http::UploadedFile:0x10339d880 @headers="Content-Disposition: form-data; name=\"article[comments_attributes][0][image]\"; filename=\"dh.png\"\r\nContent-Type: image/png\r\n", @original_filename="dh.png", @tempfile=#<File:/var/folders/A1/A1SUPUTUFA8BYB5j+RD2L++++TI/-Tmp-/RackMultipart20120228-21178-1vckii1-0>, @content_type="image/png">}}, "content"=>"article content"}, "commit"=>"Create Article", "authenticity_token"=>"i14YuJs4EVKr5PSEw9IwKXcTbQfOP4mjbR95C75J2mc=", "utf8"=>"\342\234\223"}
MONGODB (89ms) freedb['system.namespaces'].find({})
MONGODB (0ms) freedb['articles'].insert([{"name"=>"New article", "comments"=>[{"name"=>"Comment 1", "_id"=>BSON::ObjectId('4f4daf6a58001652ba000012'), "content"=>"comment content...."}], "_id"=>BSON::ObjectId('4f4daf6958001652ba000011'), "content"=>"article content"}])
Run Code Online (Sandbox Code Playgroud)
父模型:
class Article
include Mongoid::Document
field :name, :type => String
field :content, :type => String
embeds_many :comments
accepts_nested_attributes_for :comments
end
Run Code Online (Sandbox Code Playgroud)
儿童模特:
require 'carrierwave/mongoid'
class Comment
include Mongoid::Document
field :name, :type => String
field :content, :type => String
field :image, :required => …Run Code Online (Sandbox Code Playgroud) 关于这个问题:使用 Mongoid,我可以“update_all”一次将一个值推送到多个条目的数组字段上吗?
我想问一下:
{:multi => true}这里的目的是什么?update_all通过 mongoid将值推送到数组中?因为问题是在 2010 年。谢谢。
我有三节课
class Post
include Mongoid::Document
include Mongoid::Timestamps
belongs_to :user, :inverse_of => nil
embeds_many :comments, :as => :commentable
field :content, :type => String
end
class Commment
include Mongoid::Document
include Mongoid::Timestamps
belongs_to :user, :inverse_of => nil
embedded_in :commentable, :polymoriphic => true
end
class User
has_many :posts, :dependent => :destroy
field :name, :type => String
end
Run Code Online (Sandbox Code Playgroud)
每当用户创建新评论时,我想将其内容与用户所做的最新评论进行比较.这是我的代码,用于获取用户的最新评论:
comments_user=[]
Post.where("comments.user_id" => user.id).
only(:comments).
each {|p| comments_user += p.comments.where(:user_id => user.id).to_a}
latest_comment = comments_user.sort_by{|comment| comment[:updated_at]}.reverse.first
Run Code Online (Sandbox Code Playgroud)
上面的代码给了我结果,但所采用的方法效率不高,因为我必须遍历用户已经提交的所有帖子以找到最新的评论.如果有的话,任何人都可以为我提供更有效的解决方案吗?简单来说,我有没有办法得到这个用户的所有评论?
我想限制帖子和图片之间has_many关联中关联对象的数量:
在活跃的记录中我可以做类似的事情
class post < < ActiveRecord::Base
has_many :pictures, :limit => 2
end
Run Code Online (Sandbox Code Playgroud)
但是mongoid通过限制引发异常:
无效选项:提供给关系的限制:图片.有效选项包括:as,autosave,dependent,foreign_key,order,class_name,extend,inverse_class_name,inverse_of,name,relation,validate.(Mongoid ::错误:: InvalidOptions)
有没有替代实现来重新定义:限制?
类似于nodejs的mongoose如何具有枚举验证器,其中字段的值必须是预定义数组中的值之一,Rails中的mongoid模型将如何复制此行为?
即.
field :category, type: String --> must be in one of [categoryA, categoryB, categoryC]
Run Code Online (Sandbox Code Playgroud) 我正在构建一个应用程序,该应用程序有时需要将fb中的一堆数据与我的数据库同步,所以我(尝试)使用“延迟作业”将其推送到后台。这是我的“延迟工作”类的一部分。
class FbSyncJob < Struct.new(:user_id)
require 'RsvpHelper'
def perform
user = User.find(user_id)
FbSyncJob.sync_user(user)
end
def FbSyncJob.sync_user(user)
friends = HTTParty.get(
"https://graph.facebook.com/me/friends?access_token=#{user.fb['token']}"
)
friends_list = friends["data"].map { |friend| friend["id"] }
user.fb["friends"] = friends_list
user.fb["sync"]["friends"] = Time.now
user.save!
FbSyncJob.friend_crawl(user)
end
end
Run Code Online (Sandbox Code Playgroud)
与RsvpHelper班级一起生活lib/RsvpHelper.rb。因此,在我的应用程序中的某个时候,我会Delayed::Job.enqueue(FbSyncJob.new(user.id))与一个已知的有效用户进行呼叫。我建立的工作人员甚至告诉我工作已成功完成:
1 jobs processed at 37.1777 j/s, 0 failed
但是,当我在数据库中检查用户时,他的朋友列表尚未更新。我做错了什么还是什么?非常感谢您的帮助,这使我发疯。
我正在尝试使用Mongoid3将多态添加到我的嵌入关系中。
我有一个Item必须包含我的信息的embed_one对象的类。该协议是:
-我对象的类型可以是其中之一:Calendar,Sticker,Picture,
-无论对象的类型如何,我都想通过唯一的“键”来访问它:详细信息,
例如:
pry> my_item1.detail
=> `<Picture _id: 1234>`
pry> my_item2.detail
=> `<Sticker _id: 8964>`
pry>
Run Code Online (Sandbox Code Playgroud)
首先,我尝试使用关键字as和此处描述的多态:https : //github.com/mongoid/mongoid/issues/902
例如:
class Item
include Mongoid::Document
embeds_one :detail, as: :item_slot
end
class Picture
include Mongoid::Document
embedded_in :item_slot, polymorphic: true
end
class Calendar
include Mongoid::Document
embedded_in :item_slot, polymorphic: true
end
class Sticker
include Mongoid::Document
embedded_in :item_slot, polymorphic: true
end
Run Code Online (Sandbox Code Playgroud)
然后,我尝试访问我的详细信息,但不幸的是,我收到此消息错误:
pry(main)> i = …Run Code Online (Sandbox Code Playgroud) 我正在使用带有rails4的mongoid,我需要按结果分组提供图表(统计屏幕),我正在使用下面的代码.
@comments_stats = {}
comments_data = Comment.where(:created_at.lte => Date.today-10.days).group_by {|d| d.created_at.to_date }
comments_data.map{ |a| @comments_stats[a[0].strftime("%d-%m-%y")] = a[1].size}
Run Code Online (Sandbox Code Playgroud)
它会给出类似的
{
"1-1-2014" => 2,
"3-1-2014" => 1,
"4-1-2014" => 2,
"6-1-2014" => 4
}
Run Code Online (Sandbox Code Playgroud)
但我想在下面
{
"1-1-2014" => 2,
"2-1-2014" => 0,
"3-1-2014" => 1,
"4-1-2014" => 2,
"5-1-2014" => 0,
"6-1-2014" => 4
}
Run Code Online (Sandbox Code Playgroud)
任何人都建议如何简化以上查询.
谢谢普拉萨德.
我添加了生产数据库如下:
production:
sessions:
default:
uri: <%= ENV['MONGOHQ_URL'] %>
options:
consistency: :strong
max_retries: 1
retry_interval: 0
Run Code Online (Sandbox Code Playgroud)
取决于以下文档.
我MONGOHQ_URL通过以下命令创建:
heroku config:add MONGOHQ_URL=mongodb://user:pass@server.mongohq.com:port/db_name
Run Code Online (Sandbox Code Playgroud)
但当我打开控制台时heroku run rails c.我得到以下内容:
There is a configuration error with the current mongoid.yml.
message:
No clients configuration provided.
summary:
Mongoid's configuration requires that you provide details about each client that can be connected to, and requires in the clients config at least 1 default client to exist.
resolution:
Double check your mongoid.yml to make …Run Code Online (Sandbox Code Playgroud) mongoid ×10
mongodb ×4
ruby ×2
carrierwave ×1
delayed-job ×1
deployment ×1
heroku ×1
mongohq ×1
nested-forms ×1
rails-models ×1
validation ×1