<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Brainspl.at: Nested Joins and ez-where update</title>
    <link>http://www.brainspl.at/articles/2006/10/03/nested-joins-and-ez-where-update</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Brainspl.at</description>
    <item>
      <title>"Nested Joins and ez-where update" by brthrmnss AT hot mail .com</title>
      <description>Ezra, any chance we could fix the issue with: 
&lt;br /&gt;&lt;br /&gt;
When a model has two associations to the same table with different association names, ez_find breaks down because it doesn't use the table alias for the second table.
&lt;br /&gt;&lt;br /&gt;
E.g.,
&lt;br /&gt;
class Item
belongs_to :seller, :class_name=&gt;'User'
belongs_to :buyer, :class_name=&gt;'User'
end

You can't do:
Item.ez_find(:all, :include=&gt;[:buyer, :seller]) do |item, buyer, seller|
buyer.login =~ 'Ez%'
seller.login =~ 'Kevin%'
end
&lt;br /&gt;&lt;br /&gt;
The ez condition generator creates a where clause like:
WHERE ((users.login LIKE 'Ez%') AND (users.login LIKE 'Kevin%'))
&lt;br /&gt;&lt;br /&gt;
That second one is actually aliased within the join in Rails 1.1 to "sellers_items". The ez finder should actually generate:
WHERE ((users.login LIKE 'Ez%') AND (sellers_items.login LIKE 'Kevin%'))</description>
      <pubDate>Mon, 29 Jan 2007 03:07:07 +0000</pubDate>
      <guid>urn:uuid:f7a44a1c-07f1-47c3-9cae-f77c49ed3f41</guid>
      <link>http://www.brainspl.at/articles/2006/10/03/nested-joins-and-ez-where-update#comment-3935</link>
    </item>
    <item>
      <title>"Nested Joins and ez-where update" by alexl</title>
      <description>What about SUM/COUNT/MIN/MAX, GROUP BY, and HAVING?</description>
      <pubDate>Tue, 28 Nov 2006 05:16:12 +0000</pubDate>
      <guid>urn:uuid:8468681f-c0be-4dcc-b64e-762d0a6bc5ee</guid>
      <link>http://www.brainspl.at/articles/2006/10/03/nested-joins-and-ez-where-update#comment-3780</link>
    </item>
    <item>
      <title>"Nested Joins and ez-where update" by kerforn56@wanadoo.fr</title>
      <description>Cool plugin...   as any newbrails.. I have been first into 'testing' .. unit tests OK, but could not run the  functional tests : right after fixtures init, I got :

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/test_process.rb:21:in `alias_method': undefined method `process' for class `ActionController::Base' (NameError)
...
from vendor/plugins/ez-where/test/ez_where_controller_test.rb:2
which is the line : 
require 'action_controller/test_process'

any clue ?

thanks

erwin
</description>
      <pubDate>Tue, 07 Nov 2006 09:39:56 +0000</pubDate>
      <guid>urn:uuid:72708c0b-82cb-4458-b735-4d7323f2c6ad</guid>
      <link>http://www.brainspl.at/articles/2006/10/03/nested-joins-and-ez-where-update#comment-3751</link>
    </item>
    <item>
      <title>"Nested Joins and ez-where update" by sole</title>
      <description>That's absolutely amazing!
I'm by now confined to use cakephp which is kind of rails phpified but due to php's nature I really doubt this kind of things can ever be accomplished in php.
I just can't count the days until I manage to get rid of all things php and start using these magic daily. Makes life easier.</description>
      <pubDate>Sat, 14 Oct 2006 21:13:11 +0000</pubDate>
      <guid>urn:uuid:e5a32620-9584-4c55-907f-b6447c6cd1c7</guid>
      <link>http://www.brainspl.at/articles/2006/10/03/nested-joins-and-ez-where-update#comment-3719</link>
    </item>
    <item>
      <title>"Nested Joins and ez-where update" by Ezra</title>
      <description>Ask and ye shall receive ;) That works now in trunk.</description>
      <pubDate>Sat, 07 Oct 2006 08:49:30 +0000</pubDate>
      <guid>urn:uuid:3a51b919-857b-4e76-8820-0bb2d5d199f3</guid>
      <link>http://www.brainspl.at/articles/2006/10/03/nested-joins-and-ez-where-update#comment-3712</link>
    </item>
    <item>
      <title>"Nested Joins and ez-where update" by ippa</title>
      <description>This is so cool. I thought vanilla AR was a big step up from years of unmaintainable raw SQL-queries, but this really takes it one step further =).

An idea, maybe ruby regexp:

article.title =~ /^regexp/

Could transform into mysql regexp:

SELECT * FROM article WHERE title REGEXP "^regexp"

</description>
      <pubDate>Wed, 04 Oct 2006 21:48:41 +0000</pubDate>
      <guid>urn:uuid:586be74f-6457-4615-b707-f3ebc8248ea4</guid>
      <link>http://www.brainspl.at/articles/2006/10/03/nested-joins-and-ez-where-update#comment-3709</link>
    </item>
    <item>
      <title>Nested Joins and ez-where update</title>
      <description>So Fabien has been at it again and has updated ez-where with some awesome new syntax. You can now use nested joins inside your find_where&amp;#8217;s in a much more simple way.&lt;br /&gt;&lt;br /&gt;
Jon Yurek was inspired by ez-where and built &lt;a href='http://giantrobots.thoughtbot.com/2006/9/29/an-improvement-for-querying-in-rails'&gt; squirrel &lt;/a&gt;. Well we were inspired back and decided it was time to finally finish the nested include problems in ez-where. I just egged him on and offered syntax examples but Fabien did all the work on this update and it totally brings things up to a new level. But also it leaves everything fully backwards compatible. This is only possible because we have such a huge test suite in this plugin that lets us refactor and be assured nothing is broken by new additions. Anyways, let&amp;#8217;s take a look at some new syntax shall we?&lt;br /&gt;&lt;br /&gt;
Conditions involving associations are now handled more transparently,
while also providing support for conditions on nested includes.
&lt;pre&gt;
articles = Article.find_where(:all) do |article|
  article.title =~ 'Lorem%'
  article.author.name  'Ezra'
  article.comments.user.name  &amp;#8216;Fab&amp;#8217;
end
&lt;/pre&gt;
What this does is create the correct options hash and pas it along to ActiveRecord::Base#find. So that query will result in these options to find(the empty {}&amp;#8217;s don&amp;#8217;t hurt anything and made implementation much easier):

&lt;pre&gt;
:include =&amp;gt; { :author =&amp;gt; {}, :comments =&amp;gt; { :user =&amp;gt; {} } }
:conditions =&amp;gt; ["(articles.title LIKE ? AND (authors.name = ?) 
 AND (users.name = ?))", "Lorem%", "Ezra", "Fab"]
&lt;/pre&gt;

	&lt;p&gt;To make the syntax feel even more natural you can do object comparison too.
Behind the scenes the actual association is used to deduce the right FK:&lt;/p&gt;


&lt;pre&gt;
user = User.find(1)
options = Article.find_where_options do |article|
    article.comments.user == user
end
&lt;/pre&gt;

	&lt;p&gt;which results in:&lt;/p&gt;


&lt;pre&gt;
options[:include] #=&amp;gt; { :comments =&amp;gt; { :user =&amp;gt; {} } }
options[:conditions] #=&amp;gt; ["(comments.user_id = ?)", 1]
&lt;/pre&gt;
or you can also do:
 &lt;pre&gt;
users = User.find(1, 2)

options = Article.find_where_options do |article|
  article.comments.user === User.find(1, 2)
end

options[:include] #=&amp;gt; { :comments =&amp;gt; { :user =&amp;gt; {} } }
options[:conditions] #=&amp;gt; ["(((comments.user_id IN (?))))", [1, 2]]
&lt;/pre&gt;

	&lt;p&gt;The new simplified syntax is completely backwards compatible. Also, we&amp;#8217;ve
split find_where into find_where and find_where_options. This helps to check
the generated options, especially when running tests.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;s possible to use ez_where to build the :order part of the find options.
All you need to do is call order! on the column you want to use, optionally
passing :asc or :desc (string or symbol).&lt;/p&gt;


Doing so automagically includes the right assocations:
&lt;pre&gt;
options = Customer.find_where_options do |customer|
    customer.environments.apps.name =~ '%foo%'
    customer.environments.apps.name.order! :desc
end
p options
#=&amp;gt; {:order=&amp;gt;"apps.name DESC",
  :conditions=&amp;gt;["(apps.name LIKE ?)", "%foo%"], 
  :include=&amp;gt;{:environments=&amp;gt;{:apps=&amp;gt;{}}}}
&lt;/pre&gt;
&lt;br /&gt;
You can also use very complex conditions to create custom queries:&lt;br /&gt;
&lt;pre&gt;
    options = Article.find_where_options do |article|
      article.comments.user.name == 'Fab'
      article.and( (article.c.title =~ 'A%') | (article.c.title =~ 'B%') )
      article.comments.any do 
        body =~ 'Lorem%'
        body =~ 'Ipsum%'
      end
    end

p options
#=&amp;gt; {:include=&amp;gt;{:comments=&amp;gt;{:user=&amp;gt;{}}}, 
:conditions=&amp;gt;["((users.name = ?) AND 
 ((articles.title LIKE ?) OR 
(articles.title LIKE ?)) 
AND (comments.body LIKE ? OR 
comments.body LIKE ?))", 
"Fab", "A%", "B%", "Lorem%", "Ipsum%"]}
&lt;/pre&gt;
&lt;br /&gt;&lt;br /&gt;
Have at it and have some fun. I really like these new features, it makes complex AR find calls super easy to deal with and much nicer to look at. Thanks Fabien for your hard work on it!&lt;br /&gt;

	&lt;p&gt;get it here:&lt;br /&gt;
script/plugin install svn://rubyforge.org/var/svn/ez-where&lt;/p&gt;</description>
      <pubDate>Tue, 03 Oct 2006 19:58:00 +0000</pubDate>
      <guid>urn:uuid:876ff6f2-1509-42e1-a5a6-196336fe34a2</guid>
      <author>ezmobius</author>
      <link>http://www.brainspl.at/articles/2006/10/03/nested-joins-and-ez-where-update</link>
      <category>where</category>
      <category>query</category>
      <category>activerecord</category>
      <trackback:ping>http://www.brainspl.at/articles/trackback/3708</trackback:ping>
    </item>
  </channel>
</rss>

