Ruby on rails 遗传资源与Mongoid

Ruby on rails 遗传资源与Mongoid,ruby-on-rails,ruby-on-rails-3,mongodb,mongoid,inherited-resources,Ruby On Rails,Ruby On Rails 3,Mongodb,Mongoid,Inherited Resources,有人成功地使用了Rails 3并工作了吗?有什么办法让它发生吗?我想用这两种宝石 目前我遇到: undefined method `scoped' 关于索引操作 谢谢 顺便说一句,作用域问题的解决方法是覆盖集合,如下所示: class CampaignsController < InheritedResources::Base def collection @campaigns ||= end_of_association_chain.paginate(:page =&g

有人成功地使用了Rails 3并工作了吗?有什么办法让它发生吗?我想用这两种宝石

目前我遇到:

undefined method `scoped'
关于索引操作

谢谢


顺便说一句,作用域问题的解决方法是覆盖集合,如下所示:

class CampaignsController < InheritedResources::Base

  def collection
    @campaigns ||= end_of_association_chain.paginate(:page => params[:page])
  end

end
类活动控制器params[:page])
结束
结束

但是我正在寻找一种更全面的方法

如果您只使用mongoid,那么您应该做的是覆盖继承资源中的默认集合行为。默认行为如下:

也就是说,以下几点应该可以做到:

module MongoidActions
  def collection
    get_collection_ivar || set_collection_ivar(end_of_association_chain.all)
  end
end

InheritedResources::Base.send :include, MongoidActions
您甚至可以将集合默认为分页,并在所有页面中免费分页。

非常有用的帖子

如果您的控制器不能从
InheritedResource::Base
子类化,而是必须使用class方法
inherited\u resources
,您会怎么做

class MyController < AlreadyInheritedFromController
   inherit_resources
end
class MyController
上面的monkey补丁在此设置中似乎不起作用


看起来键可能是
InheritedResources::Base.inherit_resources
,但我不清楚覆盖此方法的正确方法。如果我在这里走错了路,请更正。

或者,您可以修补Mongoid:

module MongoidScoped
  def scoped
    all
  end
end

Mongoid::Finders.send :include, MongoidScoped

这将使
inherit_resources
方法按预期工作。

以下是我所做的工作,以涵盖从
InheritedResources::Base
继承和使用
inherit_resources
语句

module InheritedResources
  module BaseHelpers
    def collection
      get_collection_ivar || set_collection_ivar(end_of_association_chain.all)
    end
  end
end
您通常将其放入初始值设定项中(我使用
config/initializers/mongoid.rb


使Mongoid 2.0.0.beta.20和继承的资源1.2.1变得友好。

谢谢Jose!!效果很好。我喜欢继承的资源,我很想尝试Mongoid——很高兴很容易让它们玩得很好。更新:到现在为止,它已经被合并到master中了,对吗?