我有一个包含两个字段的MySQL表:id,name。我必须执行如下查询:
SELECT id,
name,
5 AS tot
FROM examples在rails中,我为这个表建立了一个模型:
def Example < ActiveRecord::Base
attr_accessor :tot
end如果我尝试执行以下命令:
Example.select("examples.*, 5 as tot")对于每一行,model的变量tot为null。
为什么?
如何赋值模型变量中不存在的列的值?
发布于 2012-08-28 07:44:31
嘿,你可能想从检查SQL打印出来的内容开始。
Example.select("examples.*, 5 as tot").to_sql它将检查你实际在做什么,从那里你可以看到发生了什么。
出于好奇,为什么你不能这样做呢?我只是对你上面想要实现的目标感到困惑
tot = Example.select("examples.*, 5")发布于 2012-08-28 15:29:39
只需从模型中删除attr_accessor :tot
https://stackoverflow.com/questions/12141663
复制相似问题