如何仅从模型中选择特定属性?

时间:2020-12-18 23:48:58

I want to select only specific attributes from a model(id,name).

我想只从模型中选择特定属性(id,name)。

The SQL-command can be for example:

SQL命令可以是例如:

SELECT id,name,username FROM Users

SELECT id,name,username FROM Users

Do you know how I can handle this?

你知道我怎么处理这件事吗?

8 个解决方案

#1


10  

There's a :select option on find methods. This allows you to do:

找到方法有一个:select选项。这允许你这样做:

User.find(:all, :select => 'id, name, username')

The returned objects will be User instances with those attributes available.

返回的对象将是具有可用属性的用户实例。

Or if you really want just the values without wrapping them us as User instances. You can add a method to User to return them.

或者,如果您真的只想要这些值而不将它们包装为User实例。您可以向User添加方法以返回它们。

def self.get_ids_and_names
  self.connection.select_all("select id, name, username from users")
end

which will return an array of hashes mapping column name to the value for that row. E.g. [{'id' => 1, 'name' => 'user1', 'username' => 'username1'}, ... ]

这将返回一个散列数组,将列名映射到该行的值。例如。 [{'id'=> 1,'name'=>'user1','username'=>'username1'},...]

#2


23  

Pretty old question, but the rails 3 way would be:

相当古老的问题,但轨道3方式将是:

User.pluck(:id)

#3


3  

You can also do

你也可以这样做

User.find(:all).map(&:id)

to get a list of the user ids if you are trying to get a list of user's ids or names

如果要获取用户ID或名称列表,请获取用户ID列表

#4


1  

We can use select for symbol or string such as:

我们可以使用select作为符号或字符串,例如:

User.select("id, name, username")

or

User.select(:id, :name, :username)

#5


1  

You can also pass in an array, like so:

你也可以传入一个数组,如下所示:

Model.all.select ['id', 'title']

#6


0  

In mongoid, that would be:

在mongoid中,那将是:

User.only(:name, :username).where(id: params[:id]).first

#7


0  

SQL

SELECT id,name,username FROM Users

SELECT id,name,username FROM Users

RUBY

User.select(:id, :name, :username)

User.select(:id,:name,:username)

with condition, you can write like: User.select(:id, :name, :username).find_by(:age 22)

有条件,你可以这样写:User.select(:id,:name,:username).find_by(:22岁)

#8


0  

actually for you only need write this

实际上你只需要写这个

@user= User.select(:attributeN, :attributeN......., attributeN)
respond_to do |format|
  format.json {
    render json: @user
  }

#1


10  

There's a :select option on find methods. This allows you to do:

找到方法有一个:select选项。这允许你这样做:

User.find(:all, :select => 'id, name, username')

The returned objects will be User instances with those attributes available.

返回的对象将是具有可用属性的用户实例。

Or if you really want just the values without wrapping them us as User instances. You can add a method to User to return them.

或者,如果您真的只想要这些值而不将它们包装为User实例。您可以向User添加方法以返回它们。

def self.get_ids_and_names
  self.connection.select_all("select id, name, username from users")
end

which will return an array of hashes mapping column name to the value for that row. E.g. [{'id' => 1, 'name' => 'user1', 'username' => 'username1'}, ... ]

这将返回一个散列数组,将列名映射到该行的值。例如。 [{'id'=> 1,'name'=>'user1','username'=>'username1'},...]

#2


23  

Pretty old question, but the rails 3 way would be:

相当古老的问题,但轨道3方式将是:

User.pluck(:id)

#3


3  

You can also do

你也可以这样做

User.find(:all).map(&:id)

to get a list of the user ids if you are trying to get a list of user's ids or names

如果要获取用户ID或名称列表,请获取用户ID列表

#4


1  

We can use select for symbol or string such as:

我们可以使用select作为符号或字符串,例如:

User.select("id, name, username")

or

User.select(:id, :name, :username)

#5


1  

You can also pass in an array, like so:

你也可以传入一个数组,如下所示:

Model.all.select ['id', 'title']

#6


0  

In mongoid, that would be:

在mongoid中,那将是:

User.only(:name, :username).where(id: params[:id]).first

#7


0  

SQL

SELECT id,name,username FROM Users

SELECT id,name,username FROM Users

RUBY

User.select(:id, :name, :username)

User.select(:id,:name,:username)

with condition, you can write like: User.select(:id, :name, :username).find_by(:age 22)

有条件,你可以这样写:User.select(:id,:name,:username).find_by(:22岁)

#8


0  

actually for you only need write this

实际上你只需要写这个

@user= User.select(:attributeN, :attributeN......., attributeN)
respond_to do |format|
  format.json {
    render json: @user
  }