Ruby-on-Rails [SOLVED]: Rails selecting all columns

Ruby-on-Rails [SOLVED]: Rails selecting all columns

Home Forums Frameworks Ruby on Rails Tutorials Ruby-on-Rails [SOLVED]: Rails selecting all columns

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #246747

    Cloudy Point
    Keymaster

    QuestionQuestion

    I have an Article model class with a one-to-many connection to the User model

    class Article < ApplicationRecord
      belongs_to :user
    end
    
    class User < ApplicationRecord
      has_many :articles
    end
    

    When I run

    article = Article.find(1)
    article.user.username
    

    The last statement loads the entire table. Selecting all columns is one of the performance’s evil. This is the Rails console output:

    User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
    

    But I want just the username column.

    Is there a way to do it using the same notation (model_instance.model_instance.column)?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.