eager-loading

why is this rails association loading individually after an eager load?

混江龙づ霸主 提交于 2019-12-03 14:15:17
问题 I'm trying to avoid the N+1 queries problem with eager loading, but it's not working. The associated models are still being loaded individually. Here are the relevant ActiveRecords and their relationships: class Player < ActiveRecord::Base has_one :tableau end Class Tableau < ActiveRecord::Base belongs_to :player has_many :tableau_cards has_many :deck_cards, :through => :tableau_cards end Class TableauCard < ActiveRecord::Base belongs_to :tableau belongs_to :deck_card, :include => :card end

How do you do eager loading with limits?

送分小仙女□ 提交于 2019-12-03 11:55:43
问题 In the documentation for eager loading it is stated that: If you eager load an association with a specified :limit option, it will be ignored, returning all the associated objects: class Picture < ActiveRecord::Base has_many :most_recent_comments, :class_name => 'Comment', :order => 'id DESC', :limit => 10 end Picture.find(:first, :include => :most_recent_comments).most_recent_comments # => returns all associated comments. If this is the case then what is the best way to achieve the "limit"

How to eager load associations with the current_user?

拟墨画扇 提交于 2019-12-03 11:27:41
I'm using Devise for authentication in my Rails app. I'd like to eager load some of a users associated models in some of my controllers. Something like this: class TeamsController < ApplicationController def show @team = Team.includes(:members).find params[:id] current_user.includes(:saved_listings) # normal controller stuff end end How can I achieve this? I ran into the same issue and although everyone keeps saying there's no need to do this, I found that there is, just like you. So this works for me: # in application_controller.rb: def current_user @current_user ||= super && User.includes(

Why can't Laravel/Eloquent use JOIN for Eager Loading?

故事扮演 提交于 2019-12-03 11:00:37
问题 <?php class Cat extends Eloquent { public function user() { return $this->belongsTo('User'); } } class User extends Eloquent { public function cats() { return $this->hasMany('Cat'); } } Now: $cats = Cat::with('user')->get(); Performs 2 queries: select * from `cats` select * from `users` where `users`.`id` in ('1', '2', 'x') Why can't it just do: select * from cats inner join users on cats.user_id = users.id For those saying that there are both id columns in the table, that could be easily

Eager loading a tree in NHibernate

泄露秘密 提交于 2019-12-03 10:16:53
问题 I have a problem trying to load a tree, this is my case, I have an entity associated with itself (Hierarchic) with n levels; the question is, Can I load eagerly the entire tree using ICriteria or HQL? Thanks in advance for any help. Ariel 回答1: Yes... just set correct fetchmode. i'll include example in a minute. Example taken from here => IList cats = sess.CreateCriteria(typeof(Cat)) .Add( Expression.Like("Name", "Fritz%") ) .SetFetchMode("Mate", FetchMode.Eager) .SetFetchMode("Kittens",

Force eager loading of otherwise lazy loaded properties

孤人 提交于 2019-12-03 09:37:45
I've got a Hibernate object which's properties are all loaded lazy. Most of these properties are other Hibernate objects or PersistentSets. Now I want to force Hibernate to eager load these properties for just one time. Of course I could "touch" each of these properties with object.getSite().size() but maybe there's another way to achieve my goal. The documentation puts it like this: You can force the usual eager fetching of properties using fetch all properties in HQL. References Hibernate Core Reference Guide 19.1.7. Using lazy property fetching stephen.hanson This is an old question, but I

Fetch Type LAZY still causes Eager loading Hibernate Spring data

谁说我不能喝 提交于 2019-12-03 09:06:51
I have created a simple Spring boot project with Spring data. I have a TagGroup Entity which has one to many relation with Tags. @Entity @Table(name = "TAG_GROUP") public class TagGroup{ @OneToMany(fetch=FetchType.LAZY,mappedBy = "tagGroup") private Set<Tag> tagList; } The Tag Entity is as below @Entity @Table(name = "TAGS") public class Tag { @ManyToOne(optional = false,fetch=FetchType.LAZY) @JoinColumn(name = "TAG_GROUP_ID") private TagGroup tagGroup; } I am using Spring data extending the JPArepository and using its findAll method. The problem , the Lazy fetch doesn't work BUT Infact it is

Laravel 4.1 Eager Loading Nested Relationships with Constraints

天大地大妈咪最大 提交于 2019-12-03 06:01:59
I am trying to get Eager-Loading nested relationships, with constraints, to work. Everyone seems to be giving the same example of eager-loading nested relationships: $users = User::with('posts.comments')->get(); What I want to do instead is get all the the Users related to a post of a given id. But at the same time, I also want to get the comments associated with that post. In 4.1, I to achieve the latter, I could do: $comments = Comment::whereHas('post', function($query) { $query->whereId(1); })->get(); Is there a way to marry this two and constrain a nested relationship? Actually found it a

Why can't Laravel/Eloquent use JOIN for Eager Loading?

与世无争的帅哥 提交于 2019-12-03 04:43:32
<?php class Cat extends Eloquent { public function user() { return $this->belongsTo('User'); } } class User extends Eloquent { public function cats() { return $this->hasMany('Cat'); } } Now: $cats = Cat::with('user')->get(); Performs 2 queries: select * from `cats` select * from `users` where `users`.`id` in ('1', '2', 'x') Why can't it just do: select * from cats inner join users on cats.user_id = users.id For those saying that there are both id columns in the table, that could be easily avoided with aliases: select c.id as cats__id, c.name as cats__name, c.user_id as cats__user_id, b.id as

How do you do eager loading with limits?

廉价感情. 提交于 2019-12-03 02:22:28
In the documentation for eager loading it is stated that: If you eager load an association with a specified :limit option, it will be ignored, returning all the associated objects: class Picture < ActiveRecord::Base has_many :most_recent_comments, :class_name => 'Comment', :order => 'id DESC', :limit => 10 end Picture.find(:first, :include => :most_recent_comments).most_recent_comments # => returns all associated comments. If this is the case then what is the best way to achieve the "limit" on the loading? Let's say we're eager loading the last 10 blog posts onto the front page of a blog, we