查询后将属性添加到 ActiveRecord
Add Attribute to ActiveReccord after query
这就是我想要做的:我有一个具有属性 :name、:lastame 和 :email 的用户模型。在像下面这样的方法之后:
providers = User.find_by_sql [ query ]
我希望提供者中的所有用户都包含一个新属性 :location。
这可能吗?
在这种情况下,最简单的方法是在 SELECT 子句中简单地添加您想要的值作为列别名。
providers = User.find_by_sql "SELECT *, 'The Moon' AS location FROM users"
puts providers.first.location
# => "The Moon"
这就是我想要做的:我有一个具有属性 :name、:lastame 和 :email 的用户模型。在像下面这样的方法之后:
providers = User.find_by_sql [ query ]
我希望提供者中的所有用户都包含一个新属性 :location。
这可能吗?
在这种情况下,最简单的方法是在 SELECT 子句中简单地添加您想要的值作为列别名。
providers = User.find_by_sql "SELECT *, 'The Moon' AS location FROM users"
puts providers.first.location
# => "The Moon"