ruby-on-rails-3 – 具有不同型号条件的范围
发布时间:2020-12-17 02:16:33 所属栏目:百科 来源:网络整理
导读:我有以下代码: class Item ActiveRecord::Base belongs_to :user has_many :transactions #scope :active,lambda??endclass Transaction ActiveRecord::Base belongs_to :user belongs_to :item scope :active,where("status = 0")endclass User ActiveReco
我有以下代码:
class Item < ActiveRecord::Base belongs_to :user has_many :transactions #scope :active,lambda?? end class Transaction < ActiveRecord::Base belongs_to :user belongs_to :item scope :active,where("status = 0") end class User < ActiveRecord::Base has_many :items has_many :transactions end 我希望在模型Item中构建一个范围,以仅检索具有活动事务的记录,例如: User.find(1).items.active 解决方法
我找到了答案.它应该是这样的:
scope :active,joins(:transactions) Transaction.active 答案在这里:http://asciicasts.com/episodes/215-advanced-queries-in-rails-3 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |