Rails 4 中的 has_many 'conditions' 选项相当于什么? [英] What is the equivalent of the has_many 'conditions' option in Rails 4?

查看:37
本文介绍了Rails 4 中的 has_many 'conditions' 选项相当于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我在 Rails 4 中执行以下行的等效方法是什么?

Can someone tell me what is the equivalent way to do the following line in Rails 4?

has_many :friends, :through => :friendships, :conditions => "status = 'accepted'", :order => :first_name

我尝试了以下方法:

has_many :friends, -> { where status: 'accepted' }, :through => :friendships , :order => :first_name

但我收到以下错误:

Invalid mix of scope block and deprecated finder options on ActiveRecord association: User.has_many :friends

推荐答案

需要成为第二个参数:

class Customer < ActiveRecord::Base
  has_many :orders, -> { where processed: true }
end

http://edgeguides.rubyonrails.org/association_basics.html#scopes-for-has-许多

对更新的回应:

将订单放入块内:

has_many :friends, -> { where(friendship: {status: 'accepted'}).order('first_name DESC') }, :through => :friendships

这篇关于Rails 4 中的 has_many 'conditions' 选项相当于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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