2016-06-20 16 views
2

を使用して選択し、私はeager_loadを使用して特定の列のみを選択しようとしているが、私はそれが私の「選択」をキャンセルするという問題に直面しています。Railsの4 - eager_load

モデル:

class Timeline < ActiveRecord::Base 
    belongs_to :timeline_category, foreign_key: :timeline_category_id 
    belongs_to :user, foreign_key: :user_id 
    scope :with_relations, -> { eager_load(:timeline_category).eager_load(:user).order(created_at: :desc) 
end 

問合せ:

Timeline.select('timelines.*, users.username, timeline_categories.icon').eager_load(:timeline_category).eager_load(:user) 

私も試してみました:何らかの理由で

Timeline.select('timelines.*, users.username, timeline_categories.icon').with_relations 

、それはすべての3つのテーブルのすべての列を選択し続けます。どうすれば修正できますか?

+0

でこれらのフィールドを取得し、私は同様の問題を、持っていました私はあなただけのカスタム列を追加することができますが、この方法は、常にそれらのすべてを取得して、あなたは、それらを選択的に取り出すことができない、覚えておいてください。 – potashin

答えて

0

私はそれが可能であるかないかどうかわからないんだけど、あなたはこのような何か行うことができます:

relation = TimeLine.joins(:timeline_category, :user).select('users.username AS username, timeline_categories.icon AS categories_icon') 

をしてからrelation.each{ |timeline| puts "username: #{timeline.username} and icon: #{timeline.categories_icon}" }