Mysql Rails 2-命名范围:include、:连接并选择特定列
我想知道是否可以在命名的_范围中使用:include,但只指定要:include的特定列 目前,我使用以下方法:Mysql Rails 2-命名范围:include、:连接并选择特定列,mysql,ruby-on-rails,associations,named-scope,Mysql,Ruby On Rails,Associations,Named Scope,我想知道是否可以在命名的_范围中使用:include,但只指定要:include的特定列 目前,我使用以下方法: class ProductOverwrite < ActiveRecord::Base belongs_to :product named_scope :with_name, :include => :product def name produt.name end end class ProductOverwrite:
class ProductOverwrite < ActiveRecord::Base
belongs_to :product
named_scope :with_name, :include => :product
def name
produt.name
end
end
class ProductOverwrite:product
定义名称
产品名称
终止
终止
但是我想知道我是否可以从product表中选择特定的列,而不是选择我显然不需要的整组列。rails并不是开箱即用的 你可以“背负”这个属性
named_scope :with_product_name, :joins => :product, :select => 'product_overwrites.*, products.name as piggy_backed_name'
def product_name
read_attribute(:piggy_backed_name) || product.name
end
如果ProductOverwrite
可能没有产品,则需要左连接而不是默认的内部连接