如何仅查询特定列?

时间:2012-02-06 23:29:07

标签: mysql ruby-on-rails

典型情景:

SomeModule.find(:all, :conditions ["xyz" = ?, some_var]
SomeModule.find(:first, :conditions ["xyz" = ?, some_var]
SomeModule.find(:last, :conditions ["xyz" = ?, some_var]

Desired Equivilent:

虽然以上三个例子很棒,但我正在寻找正确的方法来询问“仅”特定列,例如:

SomeModule.find(:first_name, last_name, city :conditions ["xyz" = ?, some_var]

- 或许 - -

SomeModule.find(:only => :first_name, :last_name, :city :conditions ["xyz" = ?, some_var]

在查询中删除不必要的列的轨道方式是什么?

1 个答案:

答案 0 :(得分:4)

SomeModule.find(:select => "first_name, last_name, city" ... )

文档:ActiveRecord::QueryMethods