Rails 4 Relation未排序:具有默认范围的"has_many"关联,"includes"和"order"链接在一起 [英] Rails 4 Relation is not being sorted: 'has_many' association w/ default scope, 'includes' and 'order' chained together

查看:155
本文介绍了Rails 4 Relation未排序:具有默认范围的"has_many"关联,"includes"和"order"链接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用默认范围对模型QuizCategoryWeight施加排序顺序.目标是获取@ possible_answer.quiz_category_weights以按排序顺序返回权重.

I am trying to use a default scope to impose a sort order on the model QuizCategoryWeight. The goal is to get @possible_answer.quiz_category_weights to return the weights in sorted order.

更新: 我将问题缩小到以下事实:默认范围似乎对我有用,只要它们只有一种订购"方法,而不适用于包含"时方法与订单"方法链接在一起.但是,此链接确实适用于命名范围.

可能是我的开发环境吗?还是这可能是Rails中的错误?

Could it be my development environment? Or is this a bug in Rails perhaps?

我正在使用Windows,所以也许就是问题所在.目前在ruby 2.0.0p645(2015-04-13)[i386-mingw32]和Rails 4.2.4 ...上

I am using windows, so maybe that's the problem. Currently on ruby 2.0.0p645 (2015-04-13) [i386-mingw32] and Rails 4.2.4...

在QuizCategoryWeight上使用默认范围的以下内容似乎无效:

The following, using a default scope on QuizCategoryWeight, does not seem to work:

class QuizCategoryWeight < ActiveRecord::Base
    #trying to use a default scope, but does not work
    default_scope { includes(:quiz_category).order("quiz_categories.sort_order") }

    belongs_to :possible_answer, inverse_of: :quiz_category_weights,
        class_name: 'QuizPossibleAnswer', foreign_key: 'possible_answer_id'

    belongs_to :quiz_category

end

class QuizPossibleAnswer < PossibleAnswer
    has_many :quiz_category_weights, 
        #does not work whether the line below is used or not
        ->{ includes(:quiz_category).order("quiz_categories.sort_order") }, 
        inverse_of: :possible_answer, 
        dependent: :destroy, 
        foreign_key: 'possible_answer_id' 
end

class QuizCategory < ActiveRecord::Base
    default_scope { order :sort_order }
end  

使用命名作用域,它确实可以工作.但是,这意味着我必须在表单构建器中添加一个参数才能使用集合"f.object.quiz_category_weights.sorted".

With a named scope, it does work. However, this means that I have to add an argument to my form builder to use the collection 'f.object.quiz_category_weights.sorted'.

class QuizCategoryWeight < ActiveRecord::Base
    # named scope works...
    scope :sorted,  ->{ includes(:quiz_category).order("quiz_categories.sort_order") }

    belongs_to :possible_answer, inverse_of: :quiz_category_weights,
        class_name: 'QuizPossibleAnswer', foreign_key: 'possible_answer_id'

    belongs_to :quiz_category

end

class QuizPossibleAnswer < PossibleAnswer
    has_many :quiz_category_weights, 
        inverse_of: :possible_answer, 
        dependent: :destroy, 
        foreign_key: 'possible_answer_id' 
end

推荐答案

我认为在Rails框架中或在我的Windows版本中,默认范围使用'includes'都有一个错误.

I think there is a bug with using 'includes' with a default scope, either in the Rails framework generally or in my windows version.

但是,我发现使用'joins'确实有效.我没有使用QuizCategory中的其他任何属性,因此它也更适合我的用例:我只想使用联接表中的'sort_order'属性进行排序.

However, I've found that using 'joins' does work. I'm not using any of other the attributes from QuizCategory so it's more appropriate to my use case as well: I only want to sort using the 'sort_order' attribute from the joined table.

固定代码为:

class QuizCategoryWeight < ActiveRecord::Base
    default_scope { joins(:quiz_category).order("quiz_categories.sort_order") }

    belongs_to :quiz_category
end

这篇关于Rails 4 Relation未排序:具有默认范围的"has_many"关联,"includes"和"order"链接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆