Ruby-on-rails 已弃用的 Rails 4 has_many 警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18284606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deprecated warning for Rails 4 has_many with order
提问by shankardevy
class RelatedList < ActiveRecord::Base
extend Enumerize
enumerize :list_type, in: %w(groups projects)
belongs_to :content
has_many :contents, :order => :position
end
I have this model in my rails app which throws warning when I try to create records in console.
我的 rails 应用程序中有这个模型,当我尝试在控制台中创建记录时会发出警告。
DEPRECATION WARNING: The following options in your RelatedList.has_many :contents declaration are deprecated: :order. Please use a scope block instead. For example, the following: has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment' should be rewritten as the following: has_many :spam_comments, -> { where spam: true }, class_name: 'Comment' . (called from at /Users/shivam/Code/auroville/avorg/app/models/related_list.rb:7)
弃用警告:不推荐使用您的 RelatedList.has_many :contents 声明中的以下选项::order。请改用范围块。例如,以下内容: has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment' 应该改写如下: has_many :spam_comments, -> { where spam: true }, class_name: 'Comment' 。(从 /Users/shivam/Code/auroville/avorg/app/models/related_list.rb:7 调用)
It seems like Rails 4 has new :order syntax for use in models but I can't seem to find the documentation in Rails Guides.
似乎 Rails 4 有新的 :order 语法可用于模型,但我似乎无法在 Rails 指南中找到文档。
回答by vee
In Rails 4, :orderhas been deprecated and needs to be replaced with lambda scope block as shown in the warning you've posted in the question. Another point to note is that this scope block needs to be passed before any other association options such as dependent: :destroyetc.
在 Rails 4 中,:order已弃用,需要替换为 lambda 范围块,如您在问题中发布的警告所示。另一点要注意的是,这个范围块需要在任何其他关联选项(例如dependent: :destroy等)之前传递。
Give this a try:
试试这个:
has_many :contents, -> { order(:position) }
To specify order direction, i.e. either ascor descas @joshua-coady and @wsprujit have suggested, use:
要指定订单方向,即asc或desc如@joshua-coady 和@wsprujit 所建议的那样,请使用:
has_many :contents, -> { order 'position desc' }
or, using the hash style:
或者,使用哈希样式:
has_many :contents, -> { order(position: :desc) }
Further reference on Active Record Scopes for has_many.
回答by sfoop
It took me a while to figure out how to do order and include, I eventually found that you chain the scopestatements,
我花了一段时间才弄清楚如何进行 order 和 include,我最终发现你链接了 scope语句,
has_many :things, -> { includes(:stuff).order("somedate desc") }, class_name: "SomeThing"
回答by Wylliam Judd
Just thought I'd add that if you have any option hash arguments, they have to go after the lambda, like this:
只是想我会补充一点,如果您有任何选项哈希参数,它们必须遵循 lambda,如下所示:
has_many :things, -> { order :stuff }, dependent: :destroy
Took me a minute to figure this out myself - hopefully it helps anyone else coming to this question having the same problem.
我花了一分钟时间自己解决了这个问题 - 希望它可以帮助其他遇到同样问题的人。
回答by Dave
This works for me with Rails 4 & MongoDB
这适用于 Rails 4 & MongoDB
has_many :discounts, order: :min_amount.asc
回答by Dorian
Alternatively, you can put the orderclause on the model, for instance:
或者,您可以将order子句放在模型上,例如:
has_many :options, order: 'name' # In class Answer
Becomes
成为
has_many :options # In class Answer
default_scope { order 'name' } # In class Option
PS: I got ArgumentError: wrong number of arguments (1 for 0)when doing has_many :things, -> {}.
PS:我ArgumentError: wrong number of arguments (1 for 0)在做的时候得到了has_many :things, -> {}。

