使用Mongoid,如果我有一个帐户模型并且想要为该帐户分配具有特定角色的用户,最好是将该关系嵌入帐户,用户还是创建角色集合映射帐户到具有角色名称的用户?
我希望能够返回帐户的所有用户,并验证当前用户是否可以使用Cancan等帐户访问该帐户.
构建基于帐户< - >用户角色的关系的推荐方法是什么?用户可能属于可能具有不同角色的多个帐户,类似于Basecamp的工作方式.
我Images存储在mongoDB中.它们按"created_at"排序.
现在,当我加载一个项目时,Image.find("documentID")我想要根据订单访问之前的文档和刚好在此文档之后的文档created_at.
为此,我有两个方法和一个default_scope image.rb:
default_scope asc(:created_at)
def previous_image
self.class.last(:conditions => {:created_at.lt => created_at})
end
def next_image
self.class.first(:conditions => {:created_at.gt => created_at})
end
Run Code Online (Sandbox Code Playgroud)
在当前文档之前和之后,mongoDB中是否有本地方式来访问文档?
有没有更好的方法来创建这样的命名范围与轨道中的mongoID访问上一个和下一个项目,还考虑性能(不查询一个图像三次)?
编辑:"previous_image"应该使用last,而不是first.这解决了"跳过"项目的错误.但是,关于实现这一目标的正确方法的问题仍然存在.
返回所有嵌入文档的最有效方法是什么?
说一个用户嵌入了很多地址......在ActiveRecord中,我可以用Address.count来计算它们.这样做的嵌入式文档/ mongo版本是什么?
它的2级或更高级别的时候怎么样?产品>按>变化...我怎样才能得到所有作者所有书籍中所有章节的统计数据?如果比如说Ruby,它会怎么样?
Product has_many Pressings
Pressing has_many Variations
Product
def self.pressings
all.collect { |p| p.pressings }.flatten
end
def self.variations
self.pressings.collect { |p| p.variations }.flatten
end
end
Run Code Online (Sandbox Code Playgroud) 在我的开发环境中,即Windows 7,Ruby1.9.2p180,一切正常.
但是,在生产环境中,即使用rvm的Ubuntu 10.04,Ree 1.8.7,会产生以下错误.(我正在使用passenger-apache-module来运行应用程序.)
/home/randomapp/public_html/app/models/article.rb:14: syntax error, unexpected ':', expecting kEND field :user_id, type: Hash ^
/home/randomapp/public_html/app/models/article.rb:15: syntax error, unexpected ':', expecting kEND field :username, type: String ^
/home/randomapp/public_html/app/models/article.rb:16: syntax error, unexpected ':', expecting kEND field :title, type: String ^
/home/randomapp/public_html/app/models/article.rb:17: syntax error, unexpected ':', expecting kEND field :content, type: String ^
/home/randomapp/public_html/app/models/article.rb:18: syntax error, unexpected ':', expecting kEND field :display_content, type: String ^
Run Code Online (Sandbox Code Playgroud)
这些行的代码如下
field :user_id, type: Hash
field :username, type: String
field :title, type: String …Run Code Online (Sandbox Code Playgroud) 我有这两个型号:
class Track
include Mongoid::Document
field :artist, type: String
field :title, type: String
field :isrc, type: String
has_many :subtitles
end
class Subtitle
include Mongoid::Document
field :lines, type: Array
belongs_to :track
end
Run Code Online (Sandbox Code Playgroud)
如何检查是否存在具有某个'isrc'并且有字幕(无论有多少)的曲目?
我一直在尝试这个但似乎忽略了字幕标准:
Track.exists?(conditions: {isrc: my_isrc, :subtitles.exists => true})
Run Code Online (Sandbox Code Playgroud)
即使带有'isrc'的曲目没有字幕,它也会返回true.该怎么办?
我在带有Mongoid映射器的rails应用程序中使用MongoDB.但我不明白finders和criteria查询.例如,mongoid documentaion中的section Finders是查询Model.all,但如果我使用它(例如User.all),控制台返回条件而不是结果:
=> #<Mongoid::Criteria
selector: {},
options: {},
class: User,
embedded: false>
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用finder Model.first或Model.last,控制台返回特定结果.(User.first返回特定用户,以其字段,:email,:username和其它).为什么Model.all在文档中写道finders?如果我需要全部Users或者例如,我需要做什么Posts?
我有一个死的简单的rails应用程序w/rails4和mongoid.我可以像魅力一样创建新的数据集.但我无法更新现有数据集.
有人有这个问题吗?这是怎么回事,我做错了什么?
只是从头开始创建rails 4,ruby 2和mongoid,所有这些都来自他们的git repos:
rails new mongotest --skip-active-record
Run Code Online (Sandbox Code Playgroud)
我生成一个脚手架:
rails g scaffold things name description
Run Code Online (Sandbox Code Playgroud)
我的模型现在看起来像这样:
class Thing
include Mongoid::Document
field :name, type: String
field :description, type: String
end
Run Code Online (Sandbox Code Playgroud)
这样的控制器:
class ThingsController < ApplicationController
before_action :set_thing, only: [:show, :edit, :update, :destroy]
# GET /things
# GET /things.json
def index
@things = Thing.all
end
# GET /things/1
# GET /things/1.json
def show
end
# GET /things/new
def new
@thing = Thing.new
end
# GET /things/1/edit
def edit
end
# …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用mongoid和mongodb而不是普通数据库来浏览Michael Hartl的rails4教程.我需要设置用户登录和注销的地方,这就是我遇到的问题.下面是我用来检查用户的电子邮件是否已注册的代码.
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
# sign in and load user page
else
flash[:error] = 'Invalid email/password combination'
render 'new'
# error and re-render signin
end
Run Code Online (Sandbox Code Playgroud)
不幸的是,我遇到了一个未找到的文档,因为这是查询变为空白时返回的内容.它应该进入其他但是我得到一个rails错误页面,它返回未找到的mongoid错误文档.我如何避免这种情况并让代码执行else语句中的代码呢?
谢谢!
我有这样的模型关系:
class User
include Mongoid.Document
has_many :favorite_shows
end
class FavoriteShow
include Mongoid.Document
belongs_to :user
belongs_to :show
end
class Show
include Mongoid.Document
has_many :favorite_shows
end
Run Code Online (Sandbox Code Playgroud)
FavoriteShow是用户之间的联接表,并且使用user_id和show_id作为外键进行显示。尽管这些外键已经存在,但我仍然收到以下错误:
Problem:
When adding a(n) Show to User#favorite_shows, Mongoid could not determine the inverse foreign key to set. The attempted key was 'user_id'.
Summary:
When adding a document to a relation, Mongoid attempts to link the newly added document to the base of the relation in memory, as well as set the foreign key to link …Run Code Online (Sandbox Code Playgroud) 知道发生了什么事吗?
我只是想找到第一个对象
User.first
Run Code Online (Sandbox Code Playgroud)
并且返回始终是一个随机对象,然后我尝试
User.last
Run Code Online (Sandbox Code Playgroud)
结果是同一个对象