Using :select with :include to limit eager loading

Activerecord’s find option `:select’ does not appear to work when
:include is used to eager load associations. However, I sometimes want
to perform ready-made joins on associations to limit the results of a
query. here’s a simple example:

table university:
STRING code
INT id

table term:
STRING name
INT university_id
INT id

the associations of the models

University
has_many :terms

Term
belongs_to :university

if I want to select the terms with a given university, I can perform the
straightforward function:

Term.find(:all, :include => :university, :conditions => “university.code
= ‘stanford’”)

however, this eager loads the university, which I have no use for. In
larger examples, many extra loads are performed that take a lot of ruby
processing for information that is never used. I have been able to
improve performance by using the :joins option instead of :include, but
the more intuitive rails method seems to use :select along with
:include. Am I missing something? This doesn’t seem like the expected
behavior for the :select option.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs