How to select only specific attributes from a model?

前端 未结 8 1605
傲寒
傲寒 2021-01-02 05:20

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

The SQL-command can be for example:

SELECT id,name,username FROM Users

Do you know h

相关标签:
8条回答
  • 2021-01-02 05:55

    You can also pass in an array, like so:

    Model.all.select ['id', 'title']
    
    0 讨论(0)
  • 2021-01-02 06:01

    In mongoid, that would be:

    User.only(:name, :username).where(id: params[:id]).first
    
    0 讨论(0)
提交回复
热议问题