如何使用ruby activerecord查询选择单列
我有两个班在迁移指导下如何使用ruby activerecord查询选择单列,activerecord,ruby-1.9.2,Activerecord,Ruby 1.9.2,我有两个班在迁移指导下 class1 create_table :table1 do |t| t.integer :name end end class2 create_table :table2 do |t| t.integer :table1_id t.integer :name t.text :value end end 我想使用条件table2.table1_id=“1”和table2.name=“A”从表2中仅获取值列 如何用ru
class1
create_table :table1 do |t|
t.integer :name
end
end
class2
create_table :table2 do |t|
t.integer :table1_id
t.integer :name
t.text :value
end
end
我想使用条件table2.table1_id=“1”和table2.name=“A”从表2中仅获取值列
如何用ruby编写?
我希望得到文本值作为结果。
与sql查询相同
select value from table2 where table1_id = 1 and name = 'A';
您可以像这样应用查询来检索记录 它会给你整行作为结果,你可以遍历这个数组,得到你的 期望值 @数组=表2。其中(“表1_id=?和名称=?”,1,'A') 它会很好用的 或 @array=Table2.find(:first,:conditions=>“[table1\u id='1'和name='A']” 但是ruby警告您,此代码已被弃用,但它仍然有效 但第一个查询最好 希望它对你有用
Table2.where("table1_id = ? And name = ?",1,'A').select("value")