继承资源和 Mongoid

发布于 2024-10-15 08:39:51 字数 523 浏览 3 评论 0原文

有没有人成功拥有 Rails 3、Mongoid继承资源< /a> 工作吗?有什么技巧可以让它发生吗?我很想使用这两种宝石。

目前我遇到了:

undefined method `scoped'

关于索引操作。

谢谢!


顺便说一句,范围问题的解决方法是像这样覆盖集合:

class CampaignsController < InheritedResources::Base

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

end

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

Has anyone had success having Rails 3, Mongoid and Inherited Resources working? Any tips for making it happen? I would love to use both gems.

Currently I am running into:

undefined method `scoped'

On index actions.

Thanks!


BTW a workaround for the scoped issue is to override collection like so:

class CampaignsController < InheritedResources::Base

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

end

But I am looking for a more holistic approach

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神也荒唐 2024-10-22 08:39:51

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

https://github.com/josevalim/ herited_resources/blob/master/lib/inherited_resources/base_helpers.rb#L22-24

也就是说,以下操作应该可以解决问题:

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

InheritedResources::Base.send :include, MongoidActions

您甚至可以默认集合分页,并在所有页面中免费分页。

If you are using only mongoid, what you should do is to overwrite the default collection behavior in Inherited Resources. The default behavior is this:

https://github.com/josevalim/inherited_resources/blob/master/lib/inherited_resources/base_helpers.rb#L22-24

That said, the following should do the trick:

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

InheritedResources::Base.send :include, MongoidActions

You can even default the collection to paginate and have pagination for free in all pages.

演出会有结束 2024-10-22 08:39:51

或者,您可以修补 Mongoid:

module MongoidScoped
  def scoped
    all
  end
end

Mongoid::Finders.send :include, MongoidScoped

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

Alternatively you can patch Mongoid:

module MongoidScoped
  def scoped
    all
  end
end

Mongoid::Finders.send :include, MongoidScoped

This will make inherit_resources method work as expected.

追我者格杀勿论 2024-10-22 08:39:51

以下是我为涵盖从 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.20inherited_resources 1.2.1 友好。

Here's what I did to cover both inheriting from InheritedResources::Base and using inherit_resources statement.

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

You typically put this into an initializer (I use config/initializers/mongoid.rb).

Makes Mongoid 2.0.0.beta.20 and inherited_resources 1.2.1 friendly.

極樂鬼 2024-10-22 08:39:51

非常有帮助的帖子!

如果您的控制器无法从 InheritedResource::Base 派生子类,而是必须使用类方法 inherit_resources,您会如何执行此操作,如下所示:

class MyController < AlreadyInheritedFromController
   inherit_resources
end

上面的猴子补丁不似乎在这个设置中工作。

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

Very helpful post !

How would you do this if your controller cannot be subclassed from InheritedResource::Base but rather you have to use the class method inherit_resources, like so :

class MyController < AlreadyInheritedFromController
   inherit_resources
end

the above monkey patch does not seem to work in this setup.

It looks like the key might be InheritedResources::Base.inherit_resources but i am unclear on the correct way to overwrite this method. Please correct if i am on the wrong path here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文