在assert_valid_keys中阻止':未知密钥:: order(ArgumentError)

时间:2014-03-30 21:26:45

标签: ruby-on-rails activerecord ruby-on-rails-4

我正在运行rails 4.1.0.rc2,并且在我尝试执行时遇到ArgumentError block in assert_valid_keys': Unknown key: :order

has_many :workout_exercises, dependent: :destroy, order: "exercise_order DESC"`

我想在我的连接模型上设置一个订单约束,据我所知,这与Rails 3.2一起工作,所以我无法弄清楚发生了什么。有任何想法吗?

2 个答案:

答案 0 :(得分:21)

试试这个:

has_many :workout_exercises, dependent: :destroy,-> { order "exercise_order desc" }

更新,根据OP的评论(重新排序指令):

has_many :workout_exercises,-> { order "exercise_order desc" }, dependent: :destroy

答案 1 :(得分:2)

提供另一个例子,可以帮助那些找到这篇文章的人。

# Rails 3 Syntax (below)
has_one :ca, :class_name => 'C::A',
        :foreign_key => 'person_id',
        :conditions => ['appl_id = ? AND status = ?', 4, 'active']
# Rails 4 Syntax (below) which replaces Rails 3 Syntax (above)
has_one(:ca, -> {where app_id: '4', status: 'active'}, class_name: 'C::A', foreign_key: 'person_id')