Gem mongoid-eager-loading 在 Ruby China 的环境里面有问题

huacnlee · 2011年11月23日 · 最后由 HungYuHei 回复于 2012年02月04日 · 3237 次阅读

@flyerhzm 来看看修正一下哇,估计是和我那个 mongoid_auto_increment_id 有冲突 Controller 里面调用就会有问题

class TopicsController < ApplicationController
  def index
    @topics = Topic.last_actived.includes(:node,:user).limit(15)
    render :stream => true
  end
end
Started GET "/topics" for 127.0.0.1 at 2011-11-23 22:15:03 +0800
  Processing by TopicsController#index as HTML
MONGODB ruby_china_dev['topics'].find({:deleted_at=>nil}).limit(15).sort([["replied_at", :desc], ["created_at", :desc]])
MONGODB ruby_china_dev['nodes'].find({:_id=>{"$in"=>[8, 28, 2, 21, 1, 3, 23]}})
MONGODB ruby_china_dev['users'].find({:deleted_at=>nil, :_id=>{"$in"=>[2, 152, 3, 148, 92, 140, 1, 142]}})
Rendered topics/index.html.erb within layouts/application (42.2ms)

ActionView::Template::Error (undefined method `email' for #<Node:0x007fc8c86051b0>):
    30:       <% @topics.each do |topic| %>
    31:         <div class="topic topic_line">
    32:           <div class="pull-left avatar">
    33:             <%= user_avatar_tag(topic.user,:normal) %>
    34:           </div>
    35:           <div class="right_info">
    36:             <div class="pull-right replies">
  /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.1.1/lib/active_model/attribute_methods.rb:385:in `method_missing'
  /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.4/lib/mongoid/attributes.rb:182:in `method_missing'
  /Users/jason/work/ruby-china/app/helpers/users_helper.rb:31:in `user_avatar_tag'
  /Users/jason/work/ruby-china/app/views/topics/index.html.erb:33:in `block in _app_views_topics_index_html_erb__3285293872487625280_70250171323080'
  /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-eager-loading-0.3.1/lib/mongoid-eager-loading/mongoid/criteria.rb:12:in `each'
  /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-eager-loading-0.3.1/lib/mongoid-eager-loading/mongoid/criteria.rb:12:in `each'
  /Users/jason/work/ruby-china/app/views/topics/index.html.erb:30:in `_app_views_topics_index_html_erb__3285293872487625280_70250171323080'

mongoid 已经支持 includes 了,不再需要 mongoid-eager-loading,呵呵

貌似现在 mongoid 自带了这个功能吧,开启 idendity_map 就可以了,似乎没必要再用这个 gem,可能是冲突了

额,发现一个 bug,我修改了回复,结果出来的 flash message 是 回帖删除成功。

似乎 Mongoid 原生的 includes 和 will_paginate 也有冲突,一用 paginate 就 N+1

#4 楼 @huacnlee 应该是 will_paginate 的问题 自从升级到 mongoid2.2 以来我就换成 kamigiri 了,有时候经常会有莫名其妙的兼容性问题.. 不过好像 mongoid-eager-loading 也不支持 will_paginate 的吧,我记得以前是一用 paginate 就失效

#4 楼 @huacnlee 我印象中 mongoid 升级到 2.2 之后用 will_paginate 有问题,所以现在我也改用 kaminari 了

需要 登录 后方可回复, 如果你还没有账号请 注册新账号