This post originated from an RSS feed registered with Ruby Buzz
by Guy Naor.
Original Post: Adding More Control to the Displayed Data in AjaxScaffold
Feed Title: Famundo - The Dev Blog
Feed URL: http://devblog.famundo.com/xml/rss/feed.xml
Feed Description: A blog describing the development and related technologies involved in creating famundo.com - a family management sytem written using Ruby On Rails and postgres
Wow, my 4th post of AjaxScaffold! This time a small change in it to let you better control the query used to retireve the data into the grid.
The problem I'm trying to solve, is including a join to the query used when laoding the grid. It's especially useful when doing filtering and searching. My example is the User model that has also a UserSetting association. I want the query to include a join to the user_settings table, as I want to be able to search on the fields from the joined table.
The easiest way to search/filter on AjaxScaffold is to define in your controller the method:
def conditions_for_#{plural_name}_collectionend
The value returned from this method is assigned internally by AjaxScaffold to the :conditions option of find. But if you now try to write a condition like:
['lower(name) LIKE ? OR lower(city) LIKE ?',name,city]
With city coming from the user_settings table, it will fail, as this field isn't part of the query. So we need to also change the query to include additional query parameters. All we need to do, is add another callback like the one above, that will let us adjust the options used for the find:
def adjust_options(options)options.merge!({:joins=>'inner join user_settings on user_settings.user_id = users.id'})end
To implement the change, we need to add a call to adjustoptions before using the options in the code. Look in vendor/plugins/ajaxscaffoldp/lib/ajaxscaffoldplugin.rb for the function def #{prefix}tablesetup. Around line 200, after the code:
This is a hack, as I didn't want to do a big patch when a new and highly modified version AjaxScaffold is on it's way. Should be fine as an interim solution.