Hi, this blog is no longer maintained, my new blog is here

Ruby On Rails and a Conning Israeli entrepreneur

Rails Tip: AR eager loading and :select

The AR::Base.find support the options of :include (for adding associations as JOINs in the select) and :select (which pinpoints the columns you wish to select.
For some reason, it is impossible to do them both toghther for example

Article.find(:all, :include => :author, :select => 'authors.name')

This line will raise an exception.
Luckily, there is the select_with_include gem which enables this feature!

first, install the gem

gem install select_with_include

next, require it in your environment configuration file (or in environment.rb)

require 'include_with_select'

Now, you can use the :select option, remembering to use the table name even if a column name is not unique.
There is a single fault back, in case you want to select all the table columns, you need to write them all, one by one. yeah, it sucks.


The Web Ask eizesus.com


    follow me on Twitter

    Twiters Around

    About Me

    My photo
    I am a web developer for more than 9 years, managed, cried, coded, designed and made money in this industry. now trying to do it again.