Rails 3 - select with Include?

后端 未结 4 1085
南笙
南笙 2020-11-29 02:44

Here is a nested select with include:

@items = Item.where(\"complete = ?\", true).includes( :manufacturer, {:order=>[:supplier, :agent] })
4条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 03:11

    The problem is not solved by including a call to the 'select' method in the chain. In a similar ActiveRecord::Relation we built, calling 'includes' seems to override any call to 'select'.

    scope :active, where("hired_on < ? AND (separated_on > ? OR separated_on IS NULL)", Time.now, Time.now )
    
    scope :with_role, lambda {|roles| includes(:team_member_roles).where(:team_member_roles => {:role => roles } ) }
    
    scope :with_site_code, lambda {|site_codes| includes(:team_member_sites).where(:team_member_sites => {:site_code => site_codes } ) }
    
    TeamMember.select("team_members.email, team_members.first_name, team_members.last_name").active.with_site_code(params[:site_code]).with_role(["senior_editing", "senior_and_reg_editing"]) 
    

    As shown, the query selects all columns.

    When the 2 scopes use 'joins' instead of 'includes', the query works: only the 3 specified columns are selected.

提交回复
热议问题