Tag: mongoid

使用mongoid在mongodb中使用mongodb嵌入消息回复

我认为最好的做法是将回复嵌入到该消息中的特定消息中,并且我正在尝试使用mongoid来实现它。 这就是我所拥有的 class Message include Mongoid::Document include Mongoid::Timestamps include Mongoid::Paranoia field :subject field :body field :sender_deleted, :type => Boolean, :default => false field :recipient_deleted, :type => Boolean, :default => false field :read_at, :type => DateTime referenced_in :sender, :class_name => “User”, :inverse_of => :sender, :foreign_key => ‘sender_id’ referenced_in :recipient, :class_name => “User”, :inverse_of => :recipient, :foreign_key […]

有关从MongoMapper迁移到Mongoid的建议?

看起来Mongoid现在是基于性能和开发活动的Mongo的优秀ORM。 不幸的是,我们使用MongoMapper并需要迁移。 我们应该注意哪些问题或绊脚石? 我们在Google上发现了一些过时的文章,并尝试在Mongoid Google网上论坛上发帖(虽然我们被禁止),但是会喜欢过去做过这些的SO成员的想法。 我们在Rails 3.2.12上。 谢谢!

蒙古族中的暧昧关系

我正在尝试在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 邮件#show中的Mongoid :: Errors :: AmbiguousRelationship 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 […]

从Association创建的重复记录

我正在使用Mongoid,Rails和Fabrications,并完全失去了这种情况。 任何想法都非常感激,但我知道这很复杂。 我只想制作一个用户并且只有四个已加入的组,但我一直在加载八个。 这是我的代码的相关部分 @ user1 = Fabricate.build(:已注册) @ user1.joined_groups <“Gang of 13”,:type =>:custom})] 当我运行@user1.joined_groups.size我得到4,但当我执行@user1.joined_groups.map(&:name) ,我得到8条记录: # # # # # # # # (我用一个替换了所有BSON :: ObjectId(’4eab3ca5f11aac2701000009’)语句并删除了很多中间代码。 完整的代码集可在此处获取: https : //gist.github.com/1323984 大多数bizzarre只是调用map可能会导致问题。 将“刚刚创建的用户放在这些组中:” puts @ user1.joined_groups.map(&:name) 放“然后” puts @ user1.joined_groups.map(&:name) 生成这个(!): 刚刚创建了这些组的用户: 丹科尔 CA CA46 帮派13 其次是 丹科尔 CA CA46 帮派13 丹科尔 CA CA46 帮派13 […]

Rails + MongoID +简单forms关联:#Mongoid :: Relations :: Metadata的未定义方法`options’

我有以下模型,基本上是指课程和类别。 每节课可以有一个类别,每个类别都嵌入在课程中。 class Lesson include Mongoid::Document field :title, :type => String field :category, :type => String field :price, :type => Integer field :description, :type => String field :user_id, :type => String validates_presence_of :title validates_presence_of :category validates_presence_of :price validates_presence_of :user_id validates_numericality_of :price attr_accessible :title, :category, :description, :price embeds_one :category end class Category include Mongoid::Document field :name, […]

Rails:对集合而不是数据库表使用现有的模型validation规则

Rails 4,Mongoid而不是ActiveRecord(但是为了这个问题,这应该改变任何东西)。 假设我有一个带有一些validation规则的MyModel域类: class MyModel include Mongoid::Document field :text, type: String field :type, type: String belongs_to :parent validates :text, presence: true validates :type, inclusion: %w(ABC) validates_uniqueness_of :text, scope: :parent # important validation rule for the purpose of the question end 其中Parent是另一个域类: class Parent include Mongoid::Document field :name, type: String has_many my_models end 此外,我在数据库中的相关表填充了一些有效数据。 现在,我想从CSV文件导入一些数据,这可能与数据库中的现有数据冲突。 最简单的方法是为CSV中的每一行创建一个MyModel实例,并validation它是否有效,然后将其保存到数据库中(或丢弃它)。 […]

Mongoid排序模型基于数组大小,在其他Model(has_one)关系中

我有Postactivity模型,它有post_id作为Post Model的外键(has_one关系),这个Postactivity模型有Postactivity的数组。 我如何按likes对Post模型进行排序? class Post has_one :postactivity, foreign_key: :post_activity_id, class_name:”PostActivity” end class PostActivity field :likes, type: Array belongs_to :post, foreign_key: :post_id, class_name: “Post” end

基于具有has_many关联的子集合计数的查询始终显示无结果

我正在尝试查询子集合中的文档数量。 这是我的背景: class Pro include Mongoid::Document has_many :recommendations scope :lost, -> { where(:recommendation_ids => []) } scope :normal, -> { where(:recommendation_ids.ne => []) } end 和儿童collections: class Recommendation include Mongoid::Document belongs_to :pro end 所以,现在我执行: >> Pro.lost.count 0 >> Pro.all.select{|p| p.recommendations.count == 0}.count 1 我究竟做错了什么? 我也试过Pro.with_size(recommendation_ids: 0)和其他一些变化,但没什么新的。 任何想法都将受到高度赞赏,提前感谢。 Moped 2.0.1,Mongoid 4.0.0,Rails 4.0.6

Mongoid按日期分组

这是我的文件: { event_type: ‘click’, created_at: ‘2015-02-15 10:00:00’, user_id: 100 } 我需要每天使用event_type = click对唯一身份用户求和。 得到类似的东西: { count: 320, date: ‘2015-02-15’ } { count: 451, date: ‘2015-02-16’ } (注意日期格式) 我尝试了很多我在SO上找到的不同的东西,这个对我来说几乎有用 : 用Mongoid按日期分组的最佳方法我只是想不出如何修改它来完全按照我的需要做。 这是几乎做我想用Mongoid实现的原始查询: db.events.aggregate([ {$match: {event_type: ‘click’, created_at: { $gte: ISODate(‘2015-01-01T00:00:00.000Z’), $lt: ISODate(‘2016-01-01T00:00:00.000Z’) }}}, {$group: { _id: {user_id: ‘$user_id’, account_id: ‘$account_id’, created_at: { day: { $dayOfMonth: ‘$created_at’ […]

Mongo :: Error :: OperationFailure:E11000重复键错误集合:metalab_development.demands index:sn_1 dup key:{:null}(11000)

为什么我遇到错误’重复键’? 实际上,我在代码中添加了以下行: index({ sn: 1 }, { unique: true, sparse: true}) 然后我输入命令: rake db:mongoid:create_indexes 出现了错误,让我感到困惑。