【问题标题】:mongoid where with has_many relationmongoid where 与 has_many 关系
【发布时间】:2012-07-21 09:47:25
【问题描述】:

我有 3 个模型

城市.rb

has_many :places

Place.rb

has_many :reviews, as: :reviewable

以及具有我希望能够检查的属性状态的评论。

评论.rb

  belongs_to :reviewable, polymorphic: true
  state_machine :initial => :draft do #etc.

我希望能够调用 City.find("somecity").places

而不是显示所有地点,而是替换默认范围以仅查找具有“已发布”状态的评论的地点

在 activerecord 中类似于...

City.find("somecity").places.where('reviews.state' => 'published')

有没有办法在 mongoid 中做到这一点,或者我的第二个选择是将新属性合并到每当评论发布时设置为活动的位置。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 mongoid mongoid3


    【解决方案1】:

    如果您发现自己在连接方面遇到问题,您可能应该考虑对数据进行非规范化处理。基本上只是在两个地方复制数据,这在 SQL 世界中被认为不是很好,但使用 No-SQL 可以处理通常执行连接的情况。提高速度,但使用更多空间。

    为此,您在 mongoid 中有两个很棒的宝石:

    https://github.com/dzello/mongoid_alize

    请参阅博文以了解有关道德败坏的信息:http://blog.joshdzielak.com/blog/2012/05/03/releasing-mongoid-alize-comprehensive-field-denormalization-for-mongoid/

    https://github.com/logandk/mongoid_denormalize

    虽然我更喜欢前者。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-23
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多