在config/routes.rb中:
resources :posts do
resources :comments
end
resources :pictures do
resources :comments
end
Run Code Online (Sandbox Code Playgroud)
我想允许更多的事情被评论.
我目前正在使用mongoid(mongomapper并不像我想的那样与Rails 3兼容),而注释是一个嵌入式资源(mongoid还不能处理多态关系资源),这意味着我确实需要父资源为了找到评论.
有没有优雅的方法来处理以下一些问题:
在我的控制器中,我需要在找到评论之前找到父母:
if params[:post_id]
parent = Post.find(params[:post_id]
else if params[:picture_id]
parent = Picture.find(params[:picture_id]
end
Run Code Online (Sandbox Code Playgroud)
如果我开始添加更多可评论的内容,那将会变得混乱.
也url_for([comment.parent, comment])不起作用,所以我将不得不在我的Comment模型中定义一些东西,但我认为我还需要在Comment模型中定义索引路径以及可能的编辑和新路由定义.
随着我的进一步发展,我可能会遇到更多问题.
我无法想象我是第一个尝试解决这个问题的人,是否有任何解决方案可以使其更易于管理?
我正在尝试使用mpecoid的rspec,但我遇到了这个错误:
undefined method `should_receive' for ShortenedUrl:Class
Run Code Online (Sandbox Code Playgroud)
这是我的spec_helper.rb文件:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with …Run Code Online (Sandbox Code Playgroud) 我有一个我想要解决的设计问题.
我正在构建一个Rails 3应用程序,该应用程序将保存来自各种不同公司的产品.我想定义一大组字段,每个产品都可以选择适用于它的字段.
字段类型将是单行文本字段,多行文本字段,广播或选择选项,复选框选项,日期,持续时间或更自定义的内容.我需要能够根据此类型动态呈现字段以进行编辑和显示.
我目前的想法是使用MongoDB并将所有内容存储在产品的哈希中.
class Product
include Mongoid::Document
def self.field_definitions
[{ :name => :code, :label => 'Code' },
{ :name => :itinerary, :type => :multiline, :label => 'Itinerary', :category => :tour},
{ :name => :infant_age, :type => :age_range, :label => 'Infante age range', :category => :tour},
...
]
end
embedded_in :company
field :field_data, type:Hash
end
Run Code Online (Sandbox Code Playgroud)
然后渲染新的/编辑字段,如:
= form_for Product.new do |f|
= f.fields_for :field_data do |f|
%ol
- Product.field_definitions.each do |field_definition|
%li
= f.label field_definition[:name], field_definition[:label]
= render "products/edit_fields/#{field_definition[:type] …Run Code Online (Sandbox Code Playgroud) 我有一个模特
class Post
include Mongoid::Document
include Mongoid::Timestamps
embeds_one :comment
end
Run Code Online (Sandbox Code Playgroud)
我有评论课
class Comment
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :post
field :title
field :description
end
Run Code Online (Sandbox Code Playgroud)
我有另一个继承自评论的类
class RecentComment < Comment
# certain methods
end
Run Code Online (Sandbox Code Playgroud)
现在,我希望能够创建RecentComment经过post,如果我做Post.last.build_comment(:_type => "RecentComment")了新的注释不会的_type:"RecentComment",同样如果我这样做Post.last.build_recent_comment,它给了我错误说某事像undefined method build_recent_comment for Post class.如果post有references_many :comments我应该做的Post.last.build_comments({}, RecentComment),没有任何问题.但RecentComment在这种情况下,我不知道如何使用类构建对象.如果有人可以帮助那就是gr8!
注意:我正在使用 gem 'mongoid', '~> 2.0.1'
如何将任何类的对象限制为一个.我的班级看起来像:
class Speaker
include Mongoid::Document
field :name, :type => String
end
Run Code Online (Sandbox Code Playgroud)
我只是想让一个扬声器的实例.一种方法是添加验证,该验证将检查已经存在的Speaker类的对象的数量.有没有红宝石的做事方式?
我有一个使用mongoid,database_cleaner和rspec的现有项目.我尝试使用可用的active_admin补丁添加active_admin .ActiveAdmin假定它在ActiveRecord项目中,最具体的是通过它对meta_search gem的依赖.
当我去运行我的规格时,它们都会因以下错误而失败:
Failure/Error: Unable to find matching line from backtrace
ActiveRecord::ConnectionNotEstablished:
ActiveRecord::ConnectionNotEstablished
# ./spec/support/database_cleaner.rb:12:in `block (2 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
相关库的gem版本如下:
测试失败的文件,spec/support/database_cleaner.rb:
require 'database_cleaner'
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.orm = "mongoid"
end
config.before(:each) do
DatabaseCleaner.clean
end
end
Run Code Online (Sandbox Code Playgroud) 在文档中它说你可以使用inverse_of:nil但是并没有真正描述用例:http: //mongoid.org/en/mongoid/docs/relations.html#has_and_belongs_to_many
我假设它在一个对象有很多另一个对象的情况下很有用,所以你可以完全用inverse_of nil跳过那一边并节省一些存储空间吗?
例如:
class Post
has_and_belongs_to_many :tags
end
class Tag
has_and_belongs_to_many :posts, inverse_of: nil
end
Run Code Online (Sandbox Code Playgroud)
标签可能属于数百或数千个帖子,但帖子可能只有5个标签左右.
那么这是一个很好的用例吗?我想你仍然可以做到
tag.posts
Run Code Online (Sandbox Code Playgroud)
像正常一样,主要的权衡是它改变了以下的查询:
Post.find(tag.post_ids)
Run Code Online (Sandbox Code Playgroud)
成
Post.where(tag_ids: tag.id)
Run Code Online (Sandbox Code Playgroud)
如果你有一个tag_ids的索引,它似乎仍然会很快.所以也许最好的是:
class Post
has_and_belongs_to_many :tags, index: true
end
class Tag
has_and_belongs_to_many :posts, inverse_of: nil
end
Run Code Online (Sandbox Code Playgroud)
只是想检查一下我的想法.
我正在尝试在Post模型中将viewer_ids保存为user_ids,并将User模型中的Viewed_ids保存到已查看的post_ids.当使用Rspec进行测试添加/删除并访问来自User的关系时,它很有用.但是当我使用RABL来查看post-while用户数据时,它会被混淆并给我一个不明确的关系.
#Post class
belongs_to :user
has_and_belongs_to_many :viewers, class_name: 'User', inverse_of: :viewed
#User class
has_many :users
has_and_belongs_to_many :viewed, class_name: 'Post', inverse_of: :viewers
Run Code Online (Sandbox Code Playgroud)
Problem:
Ambiguous relations :posts, :viewed defined on User.
Summary:
When Mongoid attempts to set an inverse document of a relation in memory, it needs to know which relation it belongs to. When setting :user, Mongoid looked on the class Post for a matching relation, but multiples were found that could potentially match: :posts, :viewed. …Run Code Online (Sandbox Code Playgroud) 我有一种情况,我有一个父文档,我想有两种不同类型的嵌入文档:一个作为父文件,另一个作为一个可选父文件的子文件.例如:
class ParentDoc
include Mongoid::Document
embeds_many :special_docs
embeds_many :special_doc_groupings
end
class SpecialDoc
include Mongoid::Document
embedded_in :parent_doc
belongs_to :special_doc_groupings
end
class SpecialDocGrouping
include Mongoid::Document
embedded_in :parent_doc
has_many :special_docs
end
Run Code Online (Sandbox Code Playgroud)
在此示例中,SpecialDocs和SpecialDocGroupings可以不存在关系,也可以选择具有父子关系.
但是,这是一个无效的Mongoid关联,因为我们收到此错误:
Mongoid ::错误:: MixedRelations:
问题:由于嵌入了SpecialDoc,因此不允许通过关系关联从SpecialDocGrouping文档引用(n)SpecialDoc文档.
简介:为了从SpecialDocGrouping中正确访问(n)SpecialDoc,引用需要通过SpecialDoc的根文档.在一个简单的例子中,这将需要Mongoid为根存储额外的外键,在更复杂的情况下,SpecialDoc是多级深度,需要为层次结构中的每个父级存储一个密钥.
解决方案:考虑不嵌入SpecialDoc,或在应用程序代码中以自定义方式执行密钥存储和访问.
我没有看到我正在尝试创建的关联类型有什么问题,除了Mongoid不支持它的事实.
我如何自己实现这种类型的关联?
我将在一个应用程序中利用Mongoid的单一集合继承.但是,有一个地方我想禁用此功能.我正在考虑数据库迁移(使用mongoid_rails_migrations gem),我重新定义模型以使我的迁移更易于维护.在这种情况下,我希望将该_type字段视为普通属性.
如何在Mongoid中实现它?
mongoid ×10
ruby ×3
mongodb ×2
rspec ×2
activeadmin ×1
architecture ×1
polymorphism ×1
ruby-1.9.2 ×1