In order to make a single DB query, I am eager loading Posts along with their translation data (using Rails 6 and Mobility (*)), but instead it yields 2 SQL queries:
# app/models/post.rb
class Post < ApplicationRecord
extend Mobility
translates :title, :description, backend: :table
end
# app/controllers/posts_controller.rb
class PostsController < ApplicationRecord
def index
#posts = Post.eager_load(:translations).limit(3)
end
end
<%# app/views/posts/index.html.erb %>
<% #posts.each do |post| %>
<h1><%= post.title %></h1>
<div><%= post.description %></div>
<% end %>
Result:
first, all Post IDs are selected
then all attributes of posts with those IDs are returned, using WHERE IN
SELECT DISTINCT "posts"."id" FROM "posts"
LEFT OUTER JOIN "post_translations"
ON "post_translations"."post_id" = "posts"."id" LIMIT $1 [["LIMIT", 3]]
SELECT "posts"."id" AS t0_r0, "posts"."created_at" AS t0_r1, "posts"."updated_at" AS t0_r2, "post_translations"."id" AS t1_r0, "post_translations"."locale" AS t1_r1, "post_translations"."created_at" AS t1_r2, "post_translations"."updated_at" AS t1_r3, "post_translations"."title" AS t1_r4, "post_translations"."description" AS t1_r5, "post_translations"."post_id" AS t1_r6 FROM "posts"
LEFT OUTER JOIN "post_translations"
ON "post_translations"."post_id" = "posts"."id"
WHERE "posts"."id" IN ($1, $2, $3) [["id", "00060a7d-b846-5fc5-a372-1fc3462c695c"], ["id", "008db504-6fb4-5e90-bdca-4293ebe6d920"], ["id", "034944c1-4067-5ae5-89aa-4777ef14d66b"]]
How can this double SQL statement with in-memory of IDs be avoided?
(*) A note mon Mobility
The Mobility documentation has examples yielding a single SQL statement, but as pointed out by Chris Salzberg, its query API is not used at all in this example so should not be the culprit. To try to demonstrate that the issue might not be related to Mobility but Active Record itself, below is a somewhat equivalent code stripped off of Mobility, which shows the same double-querying issue (NB: this is just for demonstration purposes, as I do want to keep using Mobility):
class Post < ApplicationRecord
has_many :translations, ->{ where(locale: I18n.locale) }
%i(title description).each do |attr|
define_method(attr) do
translations.first.send(attr)
end
end
class Translation < ApplicationRecord; end
end
<%# app/views/posts/index.html.erb %>
<% Post.eager_load(:translations).limit(3).each do |post| %>
<h1><%= post.title %></h1>
<div><%= post.description %></div>
<% end %>
If incase you are trying to get some very specific attributes from your collection, then the CollectionProxy would provide you with single query. If none of the attributes(columns) are provided, it does a distinct query before doing the OUTER JOIN query.
Honestly I haven't read through the whole implementation to confirm the reasoning behind it.
But let me show you one thing.
2.5.1 :037 > Post.eager_load(:translations).each do |post|
2.5.1 :038 > puts post.title
2.5.1 :039?> end
SQL (0.5ms) SELECT "posts"."id" AS t0_r0, "posts"."title" AS t0_r1, "posts"."created_at" AS t0_r2, "posts"."updated_at" AS t0_r3, "post_translations"."id" AS t1_r0, "post_translations"."title" AS t1_r1, "post_translations"."content" AS t1_r2, "post_translations"."locale" AS t1_r3, "post_translations"."post_id" AS t1_r4, "post_translations"."created_at" AS t1_r5, "post_translations"."updated_at" AS t1_r6 FROM "posts" LEFT OUTER JOIN "post_translations" ON "post_translations"."post_id" = "posts"."id"
title 1
title 2
title 3
title 4
In this above case, you can see the eager_load does what you are expecting. The similar case where you don't mention the needed attributes, I think when it lazy evaluates, it picks up a distinct query in addition to the OUTER JOIN query
2.5.1 :040 > Post.eager_load(:translations)
SQL (0.3ms) SELECT DISTINCT "posts"."id" FROM "posts" LEFT OUTER JOIN "post_translations" ON "post_translations"."post_id" = "posts"."id" LIMIT ? [["LIMIT", 11]]
SQL (0.5ms) SELECT "posts"."id" AS t0_r0, "posts"."title" AS t0_r1, "posts"."created_at" AS t0_r2, "posts"."updated_at" AS t0_r3, "post_translations"."id" AS t1_r0, "post_translations"."title" AS t1_r1, "post_translations"."content" AS t1_r2, "post_translations"."locale" AS t1_r3, "post_translations"."post_id" AS t1_r4, "post_translations"."created_at" AS t1_r5, "post_translations"."updated_at" AS t1_r6 FROM "posts" LEFT OUTER JOIN "post_translations" ON "post_translations"."post_id" = "posts"."id" WHERE "posts"."id" IN (?, ?, ?, ?) [["id", 1], ["id", 2], ["id", 3], ["id", 4]]
=> #<ActiveRecord::Relation [#<Post id: 1, title: nil, created_at: "2021-07-08 12:42:13", updated_at: "2021-07-09 15:32:48">, #<Post id: 2, title: nil, created_at: "2021-07-09 15:33:50", updated_at: "2021-07-09 15:33:50">, #<Post id: 3, title: nil, created_at: "2021-07-09 15:33:55", updated_at: "2021-07-09 15:33:55">, #<Post id: 4, title: nil, created_at: "2021-07-09 15:33:57", updated_at: "2021-07-09 15:33:57">]>
Hope this is someway helpful. Please post if any comments are there, so I can clarify if I could. :)
Related
What I want to solve
I want to combine the Area Model, Shop Model, and Account Model and get the results sorted in order of proximity from the location specified by the Geocoder Gem.
I want to list the results using each in a view.
However, I get a no such column: distance error as shown below.
In this case, no error will occur.
irb(main):043:0> Area.eager_load(:shop).near("tokyo")
SQL (0.4ms) SELECT areas.*, (69.09332411348201 * ABS(areas.latitude - 35.6828387) * 0.7071067811865475) + (59.836573914187355 * ABS(areas.longitude - 139.7594549) * 0.7071067811865475) AS distance, CASE WHEN (areas.latitude >= 35.6828387 AND areas.longitude >= 139.7594549) THEN 45.0 WHEN (areas.latitude < 35.6828387 AND areas.longitude >= 139.7594549) THEN 135.0 WHEN (areas.latitude < 35.6828387 AND areas.longitude < 139.7594549) THEN 225.0 WHEN (areas.latitude >= 35.6828387 AND areas.longitude < 139.7594549) THEN 315.0 END AS bearing, "areas"."id" AS t0_r0, "areas"."prefectures" AS t0_r1, "areas"."municipalities" AS t0_r2, "areas"."house_number" AS t0_r3, "areas"."building_name" AS t0_r4, "areas"."postal_code" AS t0_r5, "areas"."created_at" AS t0_r6, "areas"."updated_at" AS t0_r7, "areas"."latitude" AS t0_r8, "areas"."longitude" AS t0_r9, "shops"."id" AS t1_r0, "shops"."shop_name" AS t1_r1, "shops"."created_at" AS t1_r2, "shops"."updated_at" AS t1_r3, "shops"."shop_type_id" AS t1_r4, "shops"."station_id" AS t1_r5, "shops"."area_id" AS t1_r6 FROM "areas" LEFT OUTER JOIN "shops" ON "shops"."area_id" = "areas"."id" WHERE (areas.latitude BETWEEN 35.3933751337783 AND 35.972302266221696 AND areas.longitude BETWEEN 139.40308602609107 AND 140.11582377390894) ORDER BY distance ASC LIMIT ? [["LIMIT", 11]]
In this case, an error will occur
I want this to work.
irb(main):045:0> Area.eager_load(shop: :accounts).near("tokyo")
SQL (3.3ms) SELECT DISTINCT "areas"."id" FROM "areas" LEFT OUTER JOIN "shops" ON "shops"."area_id" = "areas"."id" LEFT OUTER JOIN "accounts" ON "accounts"."shop_id" = "shops"."id" WHERE (areas.latitude BETWEEN 35.3933751337783 AND 35.972302266221696 AND areas.longitude BETWEEN 139.40308602609107 AND 140.11582377390894) ORDER BY distance ASC LIMIT ? [["LIMIT", 11]]
Traceback (most recent call last):
ActiveRecord::StatementInvalid (SQLite3::SQLException: no such column: distance)
Environment
Ruby 2.6.3
Rails 6.0.3
Geocoder Gem: <https://github.com/alexreisner/geocoder>
Model association
# app/models/area
has_one :shop
# app/models/shop
belongs_to :shop_type
belongs_to :area
belongs_to :station
has_many :accounts
has_many :posts
# app/models/account
belongs_to :shop, optional: true
has_many :post
Addition
I tried the below, but it failed.
irb(main):001:0> Area.near("tokyo").eager_load(shop: :accounts)
(0.9ms) SELECT sqlite_version(*)
SQL (0.4ms) SELECT DISTINCT "areas"."id" FROM "areas" LEFT OUTER JOIN "shops" ON "shops"."area_id" = "areas"."id" LEFT OUTER JOIN "accounts" ON "accounts"."shop_id" = "shops"."id" WHERE (areas.latitude BETWEEN 35.3933751337783 AND 35.972302266221696 AND areas.longitude BETWEEN 139.40308602609107 AND 140.11582377390894) ORDER BY distance ASC LIMIT ? [["LIMIT", 11]]
Traceback (most recent call last):
ActiveRecord::StatementInvalid (SQLite3::SQLException: no such column: distance)
irb(main):002:0> Area.near("tokyo").eager_load(shop: :accounts)
SQL (0.3ms) SELECT DISTINCT "areas"."id" FROM "areas" LEFT OUTER JOIN "shops" ON "shops"."area_id" = "areas"."id" LEFT OUTER JOIN "accounts" ON "accounts"."shop_id" = "shops"."id" WHERE (areas.latitude BETWEEN 35.3933751337783 AND 35.972302266221696 AND areas.longitude BETWEEN 139.40308602609107 AND 140.11582377390894) ORDER BY distance ASC LIMIT ? [["LIMIT", 11]]
Traceback (most recent call last):
ActiveRecord::StatementInvalid (SQLite3::SQLException: no such column: distance)
irb(main):003:0>
The documentation mentions,
You cannot use the near scope with another scope that provides an
includes option because the SELECT clause generated by near will
overwrite it (or vice versa).
Instead of using includes to reduce the number of database queries,
try using joins with either the :select option or a call to preload.
For example:
So, maybe try Area.near("tokyo").eager_load(shop: :accounts) or Area.near("tokyo").joins(shop: :accounts) and see if that works?
I have an easy many to many relation and It doesn't work and I cannot understand why. I'm sure that is something so obvious... but..
class Content < ApplicationRecord
has_many :content_brands
has_many :brands, through: :content_brands
end
class ContentBrand < ApplicationRecord
belongs_to :content
belongs_to :brand
end
class Brand < ApplicationRecord
establish_connection Rails.application.config.brands_database_configuration
has_many :content_brands
has_many :contents, through: :content_brands
end
But
irb(main):002:0> Content.first.brands
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERRORE: la relazione "content_brands" non esiste
LINE 1: SELECT "brands".* FROM "brands" INNER JOIN "content_brands"...
^
: SELECT "brands".* FROM "brands" INNER JOIN "content_brands" ON "brands"."id" = "content_brands"."brand_id" WHERE "content_brands"."content_id" = $1 ORDER BY "brands"."name" ASC LIMIT $2
The table exists, I can query it
irb(main):006:0> ContentBrand.first.brand
ContentBrand Load (0.5ms) SELECT "content_brands".* FROM "content_brands" ORDER BY "content_brands"."id" ASC LIMIT $1 [["LIMIT", 1]]
Brand Load (27.4ms) SELECT "brands".* FROM "brands" WHERE "brands"."id" = $1 ORDER BY "brands"."name" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
=> #<Brand id: 1, name: "Nokia", logo: "nokia.jpeg", created_at: "2016-12-08 15:50:48", updated_at: "2017-02-02 15:51:43", web_site: "http://www.nokia.it">
Why?
I'm getting crazy because the inverse relation works
Brand.first.contents
Brand Load (25.8ms) SELECT "brands".* FROM "brands" ORDER BY "brands"."name" ASC LIMIT $1 [["LIMIT", 1]]
Content Load (0.7ms) SELECT "contents".* FROM "contents" INNER JOIN "content_brands" ON "contents"."id" = "content_brands"."content_id" WHERE "content_brands"."brand_id" = $1 ORDER BY "contents"."published_at" DESC LIMIT $2 [["brand_id", 391], ["LIMIT", 11]]
=> #<ActiveRecord::Associations::CollectionProxy []>
irb(main):011:0>
Update: I forgot to tell you that Brand is on another database...
You can't setup associations to a model that is stored in another database in ActiveRecord. Which makes sense since you can't join another database in a single query in Postgres without jumping through some pretty serious hoops (Postgres_FDW). And with the polyglot nature of ActiveRecord this would just be too much complexity for a very limited use case.
If its in any way possible I would switch to a single database setup even if it means that you have to duplicate data.
If you look at the "inverse query" you can see that it works because its not a single query:
# queries the "brands" database
Brand Load (25.8ms) SELECT "brands".* FROM "brands" ORDER BY "brands"."name" ASC LIMIT $1 [["LIMIT", 1]]
# queries your main database
Content Load (0.7ms) SELECT "contents".* FROM "contents" INNER JOIN "content_brands" ON "contents"."id" = "content_brands"."content_id" WHERE "content_brands"."brand_id" = $1 ORDER BY "contents"."published_at" DESC LIMIT $2 [["brand_id", 391], ["LIMIT", 11]]
However this does not mean that the concept is feasible.
I am trying to get acts as taggable on setup to work with my Rails 5 app.
I have a model called Randd::Field.rb, which has an attribute called :title in it. I want to use those titles as tags for my proposal model.
My Proposal.rb has:
class Proposal < ApplicationRecord
include Statesman::Adapters::ActiveRecordQueries
acts_as_taggable
acts_as_taggable_on :randd_maturities, :randd_fields, :randd_purposes, :randd_activities
My proposal form.html.erb has:
<%= f.collection_select :randd_field_list, Randd::Field.order(:title), :id, :title, {}, {multiple: true} %>
My proposal controller whitelists the randd_field_list with:
def proposal_params
params.require(:proposal).permit(:title, :randd_maturities_list, :randd_field_list, :randd_purposes_list, :randd_activities_list)
When I save all of this and try to add a tag to a proposal, I get an error. The server log shows:
Unpermitted parameter: randd_field_list
This doesn't make any sense because its a problem that comes up when the attribute is not whitelisted in the permitted params - which I have done.
Can anyone see what needs to be done in order to save tags via the proposal form?
I find the gem documentation confusing because it shows:
Setup
class User < ActiveRecord::Base acts_as_taggable # Alias for
acts_as_taggable_on :tags acts_as_taggable_on :skills, :interests
end
class UsersController < ApplicationController def user_params
params.require(:user).permit(:name, :tag_list) ## Rails 4 strong params usage end end
Why doesn't the permitted params include a :skill_list and an :interest_list given that those specific models are used as tags? Am I supposed to add "tag_list" to my proposal permitted params even when the only tags that can be added are from the titles defined in the specific models I've listed in:
acts_as_taggable_on :randd_maturities, :randd_fields, :randd_purposes, :randd_activities
The server log shows:
ActsAsTaggableOn::Tagging Load (1.6ms) SELECT "taggings".* FROM "taggings" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 [["taggable_id", 17], ["taggable_type", "Proposal"]]
ActsAsTaggableOn::Tag Load (1.7ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'randd_fields' AND taggings.tagger_id IS NULL) [["taggable_id", 17], ["taggable_type", "Proposal"]]
ActsAsTaggableOn::Tagging Load (2.8ms) SELECT "taggings".* FROM "taggings" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 [["taggable_id", 17], ["taggable_type", "Proposal"]]
ActsAsTaggableOn::Tag Load (3.0ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'randd_fields' AND taggings.tagger_id IS NULL) [["taggable_id", 17], ["taggable_type", "Proposal"]]
Randd::Field Load (1.6ms) SELECT "randd_fields".* FROM "randd_fields" ORDER BY "randd_fields"."title" ASC
Processing by ProposalsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"BF7l9/0QTVN3A==", "proposal"=>{"title"=>"asdf", "byline"=>"asdf", , "randd_field_list"=>["", "1"],
Unpermitted parameter: randd_field_list
You send an array for *_list parameters. Try to add [] for every _list in permit method:
params.require(:proposal).permit(
:title,
randd_maturities_list: [],
randd_field_list: [],
randd_purposes_list: [],
randd_activities_list: [])
I'm running some tests using minitest in Rails 3. I have a requirements model and a ind_requirements model which serves as an intersection between the industries model and requirements model. I have a dependent :destroy set on the has_many on the requirements model, but removing that doesn't seem to have any impact on what I'm seeing.
This is all controller testing, so I'm not interacting with the UI or I would suspect something like what is reported here: Active record update_attribute executes a DELETE query on an associated object(child) of an object(parent) when parent is updated
When the #requirement.update_attributes is hit, then I get the following output in the log:
Processing by RequirementsController#update as HTML
Parameters: {"requirement"=>{"reqText"=>"second version", "reqTitle"=>"Second Version"}, "id"=>"1"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
SQL (0.1ms) SELECT "requirements"."id" AS t0_r0, "requirements"."reqTitle" AS t0_r1, "requirements"."reqText" AS t0_r2, "requirements"."created_at" AS t0_r3, "requirements"."updated_at" AS t0_r4, "requirements"."category_id" AS t0_r5, "requirements"."status" AS t0_r6, "requirements"."source_id" AS t0_r7, "requirements"."sortOrder" AS t0_r8, "requirements"."version" AS t0_r9, "requirements"."active" AS t0_r10, "categories"."id" AS t1_r0, "categories"."catName" AS t1_r1, "categories"."created_at" AS t1_r2, "categories"."updated_at" AS t1_r3, "categories"."catAbbr" AS t1_r4 FROM "requirements" LEFT OUTER JOIN "categories" ON "categories"."id" = "requirements"."category_id" WHERE "requirements"."active" = 't' AND "requirements"."id" = ? ORDER BY categories.catName LIMIT 1 [["id", "1"]]
(0.2ms) SELECT COUNT(*) FROM "roles" INNER JOIN "role_assignments" ON "roles"."id" = "role_assignments"."role_id" WHERE "role_assignments"."user_id" = 1 AND (roleName = 'Editor')
(0.2ms) SELECT COUNT(*) FROM "roles" INNER JOIN "role_assignments" ON "roles"."id" = "role_assignments"."role_id" WHERE "role_assignments"."user_id" = 1 AND (roleName = 'Administrator')
(0.2ms) SAVEPOINT active_record_1
Industry Load (0.2ms) SELECT "industries".* FROM "industries" INNER JOIN "ind_requirements" ON "industries"."id" = "ind_requirements"."industry_id" WHERE "ind_requirements"."requirement_id" = 1 ORDER BY indName
SQL (0.2ms) DELETE FROM "ind_requirements" WHERE "ind_requirements"."requirement_id" = 1 AND "ind_requirements"."industry_id" = 2
SQL (0.5ms) INSERT INTO "requirement_versions" ("category_id", "created_at", "reqText", "reqTitle", "req_id", "status", "updated_at", "version") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["category_id", nil], ["created_at", Wed, 12 Mar 2014 06:00:13 UTC +00:00], ["reqText", "This is a test general requirement for the purpose of, um, testing things"], ["reqTitle", "Test Requirement 19"], ["req_id", 1], ["status", "Public"], ["updated_at", Wed, 12 Mar 2014 06:00:13 UTC +00:00], ["version", 1]]
(0.1ms) UPDATE "requirements" SET "reqText" = 'second version', "reqTitle" = 'Second Version', "version" = 2, "updated_at" = '2014-03-12 06:00:13.418730' WHERE "requirements"."id" = 1
(0.1ms) RELEASE SAVEPOINT active_record_1
Note the DELETE FROM. This then breaks my test as once these records are deleted from the test database, my subsequent code doesn't work properly. This delete is not triggered when running in development, just in test.
Is there a config option I'm missing?
Here is the requirement.rb file (without all of the other methods that aren't part of this):
class Requirement < ActiveRecord::Base
has_many :ind_requirements, dependent: :destroy
has_many :requirement_versions
has_many :industries, through: :ind_requirements
has_many :responses
belongs_to :category
scope :active, where(:active => true)
default_scope active.includes(:category).order('categories.catName')
scope :submitted, where(:status => :Submitted)
scope :public, where(:status => :Public)
accepts_nested_attributes_for :requirement_versions
attr_accessible :reqText, :reqTitle, :industry_ids, :category_id, :catName, :status, :user_id, :source_id, :catAbbr, :reqNumber, :sortOrder, :requirement_versions_attributes
Here is the actual test case:
test "When a requirement is reverted, the previous associated industry list is restored" do
login_admin
#requirement = create(:requirement)
#industry = create(:industry, indName: "First Industry")
#indLink = create(:ind_requirement, requirement_id: #requirement.id)
#indLink.save!
put :update, id: #requirement, requirement: { reqText: "second version", reqTitle: "Second Version"}
#industry = create(:industry, indName: "Second Industry")
#indLink.industry_id = #industry.id
#indLink.save!
get :revert, id: #requirement
#requirement = Requirement.find(#requirement.id)
#indLink = #requirement.ind_requirements.first
assert_equal(#indLink.industry.indName, "First Industry")
end
The problem is that when I was sending the update to the requirements, I was only sending the updated parameters: put :update, id: #requirement, requirement: { reqText: "second version", reqTitle: "Second Version"}, but not including the parameters for the checkboxes, as I wasn't modifying them. Unfortunately, the end result is that the industries are then deleted. I imagine because the absence of parameters is interpreted not that they aren't being updated, but rather that they have been deleted.
I have a scope:
includes(:countries).where("profiles.sector = :sector OR advices.sector = :sector", :sector => sector)
It produces the following SQL:
SELECT `profiles`.* FROM `profiles` INNER JOIN `advices` ON `advices`.`profile_id` = `profiles`.`id` WHERE (profiles.sector = 'Forestry_paper' OR advices.sector = 'Forestry_paper')
(yes I have country in my Profile and in my Country model)
Unfortunately, the OR seems to fail:
it doesn't render a profile having only the proper sector but no related advice. Thoughts?
You are doing an INNER JOIN, so it requires that the profiles have a corresponding advice. Try the following instead:
Profile
.joins("LEFT JOIN advices ON advices.profile_id = profiles.id")
.where("profiles.sector = :sector OR advices.sector = :sector", :sector => sector)
This will also include profiles that have no advices.
You can do outer joins by specifying a where clause with a hash after the includes:
Post.includes(:comments).where(:comments=>{:user_id=>nil})
produces:
Post Load (0.5ms) SELECT "posts"."id" AS t0_r0, "posts"."created_at" AS t0_r1,
"posts"."updated_at" AS t0_r2, "comments"."id" AS t1_r0, "comments"."user_id"
AS t1_r1, "comments"."post_id" AS t1_r2, "comments"."content" AS t1_r3,
"comments"."created_at" AS t1_r4, "comments"."updated_at" AS t1_r5
FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id"
WHERE ("comments"."user_id" IS NULL)
Ryan Bigg wrote a helpful blog post about this.
EDIT
Be aware that this technique is more or less a side effect of the way Rails constructs the SQL for eager-loading associations. An explicit LEFT JOIN is more robust, as suggested in the accepted answer.
Check out http://metautonomo.us/projects/metawhere/ for more query goodness...
meta_where is now unmaintained: https://github.com/activerecord-hackery/meta_where
Rails 5 is introducing OR statements:
Rails 5: ActiveRecord OR query