-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Does not work with includes or eager_loads #4
Comments
Bit outdated but for the record. |
I don't know what is right solution would be. I don't want to reproduce rails' logic for For one to one / belongs to:User.select("users.*, (select name from cities where user.hometown_id = cities.id) as hometown_name").... For one to many / many to many:users = User.all.light_records
photos = Photo.where(user_id: users.map(&:id)).light_records
users.each do |user|
user_photos = photos.select {|ph| ph.user_id = user.id }
...
end If your volumes are really big then can process in batches:batch = []
User.all.light_records_each do |user|
batch << user
if batch.size >= 1000
photos = Photo.where(user_id: batch.map(&:id)).light_records
process_batch(batch, photos)
end
end
photos = Photo.where(user_id: batch.map(&:id)).light_records
process_batch(batch, photos) If you want to have same interface as rails associations then can make attr_accessor:class User < ActiveRecord::Base
module LightRecord
attr_accessor :photos
end
end
users = User.all.light_records
photos = Photo.where(user_id: users.map(&:id)).light_records
users.each do |user|
# to make same interface as rails' preloading
user.photos = photos.select {|ph| ph.user_id = user.id }
...
end |
@Paxa Right, thank you. |
Method I can suggest you to rewrite code to load relations manually, like this: articles = Article.all.light_records
users = {}
User.find_by(id: articles.map(&:user_id).uniq).light_records_each do |user|
users[user.id] = user
end
articles.each do |article|
user = users[article.user_id]
...
end |
I found out that it doesn't work when using includes or eager_loads, however joins and preload works correctly. This is not a problem because the performance improvements are so great that I am able to work around these. But if this can't be fixed it would be nice to note this in the readme.
The text was updated successfully, but these errors were encountered: