ActiveRecord: check whether association exists without loading it? - activerecord

Suppose I've got ActiveRecord models such that User has_one :photo. In the database, photos has a t.binary column which may hold a lot of data, so I don't want to SELECT that column unless I need to.
I want to do something like:
users.each do |user|
image_tag(user_photo_path) if user.photo.present?
end
However, I don't want to call user.photo.present? because:
Doing so loads the photo association, including SELECT * from photos
Even if it could be made to only SELECT id FROM photos to check existence, it's still an N + 1 query.
What I really want is to load users with a single query which gives each one a property telling me whether it has an associated photo or not.

With ActiveRecord 5, this works:
class User < ActiveRecord::Base
scope :with_photo_id, -> {
left_outer_joins(:photo).select(
"users.*, photos.id AS photo_id"
)
}
end
Then I can call User.with_photo_id and check user.photo_id.present?.
Prior to AR 5, the join would be uglier:
joins(
"LEFT OUTER JOIN photos ON photos.user_id = users.id"
)

Related

Access an ActiveRelation in a view

I have two models with the appropriate foreign key created in the people table:
class Person < ActiveRecord::Base
belongs_to :family
class Family < ActiveRecord::Base
has_many :people
If I do the following I get an object - #family_members - as an instance variable and I have no problems:
#family_members = Family.find(1)
I can access the 'child' people table fields easily in my view:
#family_members.people.first_name
However, if I use the arel way with "where" etc. I get an "ActiveRecord::Relation", not a normal object, which leaves me stumped as to how to access the same "first_name" field form the people table like I accessed above:
#family_members = Family.where(:id => 1)
or even
#family_members = Family.joins(:people).where(:id => 1)
(is the "joins" even required??)
I understand that using ".first" will cause the query to run:
#family_members = Family.where(:id => 1).first
But it returns an array, not an object, so if I use in my view:
#family_members.people.first_name
I get a "method 'people' unknown" error.
How can I access the 'first_name' field of the people table like I did with the object created by "find" but using an ActiveRecord relation?
* added information 7/15 ********
To clarify what I am looking for -- here is what I would have written if I were writing SQL instead of Arel:
SELECT f.home_phone, f.address, p.first_name, p.last_name, p.birthday
FROM families f INNER JOIN people p ON p.family.id = f.id WHERE family_id = 1
With that query's results loaded into a result set I could access:
myResultSet("home_phone") -- the home_phone from the families table
myResultSet("address") -- the address from the families table
myResultSet("first_name") -- the first_name from the people table
myResultSet("birthdate") -- the birthdate from the people table
If the two tables in the query have a same-named field I would just use "AS" to request one of the fields by another name.
I have used this kind of query/result set for many years in web apps and I am trying to deduce how to do the same in Rails and ActiveRecord.
#family_members.people.first_name shouldn't ever work so I'm surprised you find it working ... #family_members contains a Family object, #family_members.people is an array of Person objects.
The fact that you're calling it #family_members seems to make me think you're expecting it to be an array of Persons... in which case the correct code would be...
#family_members = Family.find(1).people # finds people in first Family object
If you expect #family_members to contain just the first family member, then...
#family_members = Family.find(1).people.first
If you want an array of first names of all family members, then...
#family_members = Family.find(1).people # finds people in 1st Family object
#family_members.map {|member| member.first_name} # array of first_name
#family_members = Family.find(1) and #family_members = Family.where(:id => 1) are functionally identical.. both retrieve the first Family object in the database in each case may contain zero, one, or multiple people.
Just to be clear, the "1" in all examples above refer to which Family object is retrieved, not which Person in the Family.

How to simplify this ActiveRecord eager-load query?

In a project of mine, I'm using the Ruby ActiveRecord (not a Rails application, though) and I use the following structure:
class Customer < ::ActiveRecord::Base
has_and_belongs_to_many :categories
end
class Categories < ::ActiveRecord::Base
has_and_belongs_to_many :customers
end
In one part of the application, I load all the customers to process them and I try to eager-load their relevant categories (within a ActiveRecord::IdentityMap.use block):
Customer.includes(:categories).all
This does what I need to do, but when I look at the resulting eager-load query, it reads like:
SELECT "categories".*, "t0"."customer_id" AS ar_association_key_name_customer_id
FROM "categories" INNER JOIN "customers_categories" "t0"
ON "categories"."id" = "t0"."category_id" WHERE
((("t0"."customer_id" = 1) OR ("t0"."customer_id" = 2) OR ("t0"."customer_id" = 3) OR ... ))
I am loading all the customers and there is no need to filter them on the join table. There are only several categories, but many customers and the resulting query has thousands of unneeded OR statements.
Is there a way to simplify the query (in the ActiveRecord way) to not include the WHERE conditions in form of customer_id = X ?

Ruby, Retrieve Child Object By Key

I am trying to retrieve a child object based on the key in its parent's table. For instance, I have the Customer class which contains a "store_id" key to the Stores tables. If a customer has a "store_id" key, I would like to bring back that Store object and not the parent Customer object.
EDIT: Here is a sql statement showing what I am trying to do.
So the SQL statement would look something like this.
"SELECT storeS.* FROM customers INNER JOIN stores ON customers.store_id = storeS.id WHERE customers.id = '9'"
I know the sql is probably wrong, but thats a very concise way to show it.
I am assuming you are using rails with the out-of-the-box configuration (using ActiveRecord).
By convention, the "store_id" key in the "customers" table should match an "id" field in the "stores" table. You should also have the following class models setup:
class Store < ActiveRecord::Base
has_many :customers # this is not required for what you want to do here, but recommended
end
class Customer < ActiveRecord::Base
belongs_to :store
end
Assuming this is true, you can either do this if you have the store key:
# assuming we have store key == 9
Store.find(key)
Or you could do this if you already have the customer:
# assuming we have customer.store_id == 9
customer.store
Or if you only have the customer key:
# assuming we have a customer key == 9
customer = Customer.find(9)
store = customer.store
I don't use ActiveRecord a lot, but I think it's this:
Store.find(customer.store_id)

Why is my has_many through associated record (sometimes) readonly?

I have three ActiveRecord models: Partner, MembershipChannel (which is an STI model, inheriting from Channel) and ChannelMembership (I was not responsible for naming these models…)
When I load a ChannelMembership through the Partner association, I sometimes(!) end up with a readonly record. This is in Rails 3.0.9. The same code did not behave this way in 2.3.11.
> p = Partner.first
> p.channel_memberships.map(&:readonly?)
# => [false, false, false, false, false, false]
> p.reload.channel_memberships.limit(1).first.readonly?
# => false
> p.reload.channel_memberships.first.readonly?
# => true
Why is readonly? true when first is called on the association, but not on the relation from limit?
I understand that readonly is triggered if I use SQL fragments when finding a record, but this isn't the case here. It is just a plain has_many through association. The only complicating matter is that it joins on an STI model. What's more, looking at the generated SQL from the last two examples, they are identical!
I can get the behaviour I want by specifying :readonly => false on the association, but I want to understand what is going on.
There are no default scopes on Channel, MembershipChannel or ChannelMembership. Here is the association declaration on Partner:
class Partner
has_many :membership_channels
has_many :channel_memberships, :through => :membership_channels
end
Here is the generated SQL from my logs:
Partner Load (0.4ms) SELECT "partners".* FROM "partners" LIMIT 1
ChannelMembership Load (0.7ms) SELECT "channel_memberships".* FROM "channel_memberships" INNER JOIN "channels" ON "channel_memberships".channel_id = "channels".id WHERE (("channels".partner_id = 2) AND (("channels"."type" = 'MembershipChannel')))
Partner Load (0.5ms) SELECT "partners".* FROM "partners" WHERE "partners"."id" = 2 LIMIT 1
ChannelMembership Load (1.0ms) SELECT "channel_memberships".* FROM "channel_memberships" INNER JOIN "channels" ON "channel_memberships".channel_id = "channels".id WHERE (("channels".partner_id = 2) AND (("channels"."type" = 'MembershipChannel'))) LIMIT 1
Partner Load (0.4ms) SELECT "partners".* FROM "partners" WHERE "partners"."id" = 2 LIMIT 1
ChannelMembership Load (0.6ms) SELECT "channel_memberships".* FROM "channel_memberships" INNER JOIN "channels" ON "channel_memberships".channel_id = "channels".id WHERE (("channels".partner_id = 2) AND (("channels"."type" = 'MembershipChannel'))) LIMIT 1
I was able to reproduce your problem through a basic has_many :through association and am also as to what's causing it.
From what I can tell, it only happens when the reload method is called on the original object. I'm not sure if this is because of anything that reload's doing specifically, or perhaps because certain attribute flags are being reset?
My second theory is that it has something to do with the fact that
p.reload.channel_memberships.limit(1)
returns an ActiveRecord::Relation through which you obtain your first ChannelMembership, and
p.reload.channel_memberships.first
loads it directly from the association. Perhaps some combination of reload resetting certain cached items (I don't know the AR source) is flagging the association as read only. When you apply the limit(1) scope on it, it may be resetting these in a new relation, and working as you'd expect.
I'd poke around ActiveRecord::Persistence / Associations a bit more for the full answer.

Rails 3 Query: How to get most viewed products/articles/whatever?

I always wondered how to query and get results that doesn't fit in a model. Similar how it's done using LINQ and projecting into anonymous objects.
So here's the simple schema:
# Product.rb
class Product < ActiveRecord::Base
has_many :product_views
# attributes: id, name, description, created_at, updated_at
end
# ProductView.rb
class ProductView < ActiveRecord::Base
belongs_to :product
# attributes: id, product_id, request_ip, created_at, updated_at
end
Basically I need to get a list of Products (preferably just id and name) along with the count of views it had. Obviously ordered by view count desc.
This is the SQL I want to get:
select
p.id,
p.name,
count(pv.product_id) as views
from
product_views pv
inner join
products p on pv.product_id = p.id
group by
pv.product_id
order by
count(product_id) desc
I tried the following and similar, but I'm getting ProductView objects, and I would like to get just an array or whatever.
ProductView.includes(:product)
.group('product_id')
.select("products.id, products.name, count(product_id)")
This kind of thing are trivial using plain SQL or LINQ, but I find myself stucked with this kind of queries in Rails. Maybe I'm not thinking in the famous 'rails way', maybe I'm missing something obvious.
So how do you do this kind of queries in Rails 3, and specifically this one? Any suggestions to improve the way I'm doing this are welcome.
Thank you
You can use Arel to do what you're looking for:
products = Product.arel_table
product_views = ProductView.arel_table
# expanded for readability:
sql = products.join(product_views)
.on(product_views[:product_id].eq(product[:id]))
.group(product_views[:product_id])
.order('views DESC')
.project(products[:id],
products[:name],
product_views[:id].count.as('views'))
products_with_views = Product.connection.select_all(sql.to_sql) # or select_rows to just get the values
Yes, it is long, but Arel is a very smart way to deal with creating complex queries that can be reused regardless of the database type.
Within a class method in the Product class:
Product.includes(:product_views).all.map { |p| [p.id, p.name, p.product_views.size] }
Then sort it however you want.
I don't know if there's a way to do it using your models. I would probably resort to:
Product.connection.select_rows(sql)
Which will give you an array of arrays. You can use select_all if you'd rather have an array of hashes.
Try this:
#product = Product.find(#product_id)
#product_views = #product.product_views.count
(Source - http://ar.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html#M000292)
Hope this helps!

Resources