svenfuchs/globalize3 — pull request #274 by shioyama (2013-10-06 03:27:31+00:00)
Title: Handle translated attribute key/values in relation queries

The rails3 version of globalize patched rails' dynamic finders, so if you had a translated `Post` with a title "my post", you could find it with:

````ruby
Post.find_by_title('my post')
````

I happened to use those methods quite often, so I was disappointed when I tried them on the rails4 branch and realized they no longer work (since rails has deprecated dynamic finders).

Instead, I had to resort to this:

````ruby
post = Post.with_translations(Globalize.locale).where("post_translations.title" => "my post").first
````

This works, but it's extremely cumbersome and goes against the whole idea that Globalize is supposed to make accessing and updating translated attributes more or less seamless. Not to mention, if this post has translations in locales other than the current one, they won't be loaded by default (you have to reload them with `post.reload`, here's [an explanation why](http://stackoverflow.com/questions/9205039/matching-nested-model-association-attribute-with-includes)).

What I really want is this:

````ruby
Post.where(title: 'my post').first
````

I want this to find the first post with a title "my post" in my current locale, and I want all translations to be loaded by default, so that if I then do `post.translation_for(:ja).title` I get the Japanese title if there is one.

This code I'm submitting here makes that work by adding a globalize relation class which inherits from `ActiveRecord::Relation` and overrides `where` and `not` so that relations on a translated model automatically convert attribute names to their equivalents on the translations table. They also override a handful of finder methods (`first`, `last` and `take`) to automatically reload translations where necessary to ensure that the record has all translations loaded, which globalize3 did in the rails3 dynamic finders patch.

So, e.g. you can do this:

````ruby
Post.where.not(title: `my post').first
````

and it will find the first post that does not have a title "my post" in the current locale, and also reload that post so that it has all translations loaded.

I've tried a few different ways of doing this but this seems the safest (not monkeypatching ActiveRecord). I'd appreciate any thoughts/suggestions.