lundi 19 décembre 2016

How to write select statement in rails controller

I want to get only specific column details from ActiveRecord. So i did run the following code.

accounts = self.accounts(:select => "id, name,account_number").order("created_at DESC").paginate(:page => page_count, :per_page => per_page_count)

Whenever this line is getting executed its firing this following query

SELECT "accounts".* FROM "accounts" INNER JOIN "super_account_accounts" ON "accounts"."id" = "super_account_accounts"."account_id" WHERE "super_account_accounts"."super_account_id" = 8 ORDER BY accounts.created_at DESC LIMIT 10 OFFSET 0

Why it is getting all the column information when i have already specified few of the columns for which i want the info.

NOTE:- Super account model has the following association with account has_many :accounts, :through => :super_account_accounts, :dependent => :destroy account model has the following association with super account has_many :super_accounts, :through => :super_account_accounts

Aucun commentaire:

Enregistrer un commentaire