以下所有示例:包括for eager loading用于类级别查询.我在我的模型实例上尝试了它,它仍然发出了一堆查询 - 它是否适用于实例方法?
#in controller
@emails = person.sent_emails(:include => [:recipient])
#in view
render @emails
# _email.html.erb partial
<h1><%= email.recipient.name %></h1>
<p>
<%= email.content %>
</p>
#still issues a select * for emails, N+1 for recipients :/
Run Code Online (Sandbox Code Playgroud) 我认为我需要一些类似于rails eager加载查询的东西,但有限制,但我找不到解决方案.
为简单起见,我们假设系统中永远不会超过30 Person秒(因此Person.all是一个小数据集),但每个人将有超过2000条评论(因此Person.include(:comments)将是一个大型数据集).
家长协会
class Person < ActiveRecord::Base
has_many :comments
end
Run Code Online (Sandbox Code Playgroud)
儿童协会
class Comment < ActiveRecord::Base
belongs_to :person
end
Run Code Online (Sandbox Code Playgroud)
我需要查询Persons 列表并包含它们comments,但我只需要其中的5个.
我想做这样的事情:
有限的家长协会
class Person < ActiveRecord::Base
has_many :comments
has_many :sample_of_comments, \
:class_name => 'Comment', :limit => 5
end
Run Code Online (Sandbox Code Playgroud)
调节器
class PersonController < ApplicationController
def index
@persons = Person.include(:sample_of_comments)
end
end
Run Code Online (Sandbox Code Playgroud)
遗憾的是,本文指出:"如果您急于加载具有指定:limit选项的关联,它将被忽略,返回所有关联的对象"
这有什么好办法吗?或者我注定要在急切加载1000个不需要的ActiveRecord对象和N + 1查询之间进行选择?另请注意,这是一个简化的示例.在现实世界中,我将与其他相关联Person,在相同的index行动中具有相同的问题comments.(照片,文章等).
任何人都可以告诉我是否可以加载关联但只返回特定属性?
我想用他们的帐户检索一些订单,但只需要帐户名称.
Order.select([:id, :account_id]).includes(:account).limit(2)
我们Ticket在 rails admin 中有一个加载非常缓慢的模型的列表视图。
class Ticket < ActiveRecord::Base
belongs_to :crew
end
Run Code Online (Sandbox Code Playgroud)
之所以慢,是因为我们crew通过rails_admin_pretty_print访问其他相关模型的方法来显示票证的关系。
class Crew < ActiveRecordBase
belongs_to :pool
belongs_to :leader
def rails_admin_pretty_print
"leader : #{leader.name} at time #{pool.datetime}"
end
end
Run Code Online (Sandbox Code Playgroud)
我想在初始查询中急切加载所有这些对象,以加快请求速度。就像是:
config.model "Ticket" do
object_label_method :rails_admin_pretty_print
list do
field :crew, includes(:pool, :leader)
end
end
Run Code Online (Sandbox Code Playgroud)
我在rails 管理文档中找不到任何方法来做到这一点。有没有办法做到这一点?
我有一个User模特.
A user有很多integrations.
An integration连接到包含列的profilevia .integration_profilesdata
我想急切加载所有用户的个人资料.
class Integration < ActiveRecord::Base
has_many :integration_profiles
has_many :profiles, through: :integration_profiles
end
class IntegrationProfile < ActiveRecord::Base
belongs_to :integration
belongs_to :profile
end
class Profile < ActiveRecord::Base
has_many :integration_profiles
has_many :integrations, through: :integration_profiles
end
Run Code Online (Sandbox Code Playgroud)
我试过这个:
all = User.first.integrations.includes(:profiles)
Run Code Online (Sandbox Code Playgroud)
但是当我这样做的时候 all.count
=> 2
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做
all = User.first.integrations.joins(:profiles)
all.count
=> the correct total
Run Code Online (Sandbox Code Playgroud)
我应该使用包含还是加入?我一直使用包括所以不确定为什么这不起作用
我被困在这里从2-3小时开始尝试.
我有很多关系:
class Category extends Model
{
public function news()
{
return $this->belongsToMany('App\News');
}
}
class News extends Model
{
public function categories()
{
return $this->belongsToMany('App\Category');
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试获取相关类别的最新5条新闻:
$front_categories = Category::with(array(
'news'=>function($query){
$query->where('publish','1')->orderBy('created_at', 'desc')->take(5);}))
->where('in_front', 1)->get();
Run Code Online (Sandbox Code Playgroud)
上面的查询对我不起作用,它总共给出了五个结果,而不是每个类别的5个结果.
那些是很多模特
class FacultyMember(models.Model):
# some attributes
class Publication(models.Model):
# some attributes
author = models.ManyToManyField(FacultyMember, blank=True)
class Project(models.Model):
# some attributes
researchers = models.ManyToManyField(FacultyMember, blank=True)
Run Code Online (Sandbox Code Playgroud)
我希望Faculty会员能够通过热切的加载获得所有相关的项目和出版物.
我尝试了一些像下面的代码
FacultyMember.objects.filter(
pk=id,
first_name=first_name,
last_name=last_name
).select_related('project_set').select_related('publication_set')
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
如何在django orm中加入相关字段并加载.
更新(解决方案)
->user关系,那么变量已经可用,因为您从中加载了!$image$user->images$user->imagesprotected $withEloquent 属性。这是一种反模式。原来的
我遇到了一个我认为很简单的问题:
User对象 ImageImage 属于 User...(逆关系)我的问题是,我想急于负荷均images()在User模型和user()开Image模式。为此,我只是$with按照文档中的说明设置了一个属性。
我的User型号:
class User extends EloquentModel {
protected $with = ['images'];
public function images()
{
return $this->hasMany(Image::class);
}
}
Run Code Online (Sandbox Code Playgroud)
我的Image型号:
class Image extends EloquentModel {
protected $with = ['user'];
public function user()
{
return …Run Code Online (Sandbox Code Playgroud) 我有两个实体之间的多对多关系:类别 <--> 项目
public class CategoryMaster
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual List<SubCategoryMaster> SubCategories { get; set; }
public List<ItemMaster> Items { get; set; }
}
public class ItemMaster
{
public long Id { get; set; }
public string Name { get; set; }
public List<CategoryMaster> Categories { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
每当我尝试将相关项目显式加载到所有/某些类别时,它都会给我
与这些类别相关的项目等等......嵌套/循环引用
db.CategoryMaster
.Include(x=>x.Items)
.Include(x=>x.SubCategories.Select(y=>y.Items))
.ToList();
Run Code Online (Sandbox Code Playgroud)因此,在使用 Json.Encode() 将其序列化为 *.cshtml 上的 JSON 时会导致以下错误;
A …Run Code Online (Sandbox Code Playgroud) asp.net-mvc json entity-framework lazy-loading eager-loading
我有一个具有灵活数据库结构的 SaaS 应用程序,因此它的字段肯定会发生变化,特别是考虑到它有一个 Json 字段(用于从应用程序的客户端创建的任何额外数据库结构),包括基于关系的字段。所以Account Table可以动态创建employee_id字段,因此需要动态访问关系
我需要基于这种动态关系 EagerLoad 模型。如果我有这样的事情:
// Account Model
public function employee(){
return $this->belongsTo(App\Employee);
}
Run Code Online (Sandbox Code Playgroud)
这很容易。但我所拥有的是:
public function modelBelongsTo(){
return $this->belongsTo($dynamicClassName, $dynamicForeignKey);
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我急切地加载它,我将在 key 上获得带有相关 Employee 的 Account Model 实例modelBelongsTo。这就是Eloquent Names如何基于eagerload的功能。但是在此之后,我无法再次使用此函数来预先加载第二个模型,因为它只会覆盖modelBelongsTo键上的结果。
1)我可以以某种方式更改laravel的进程以使用我提供的名称吗?
或者
2)我可以即时编写函数来克服这个问题,所以我会即时编写员工函数吗?
或者
3)最坏情况:我遍历所有记录以单独重命名它们的键,因为我使用的是分页,循环超过 10 条记录并不是什么大问题。
eager-loading ×10
eloquent ×2
laravel ×2
activerecord ×1
asp.net-mvc ×1
django ×1
django-orm ×1
inner-join ×1
json ×1
laravel-5 ×1
lazy-loading ×1
limit ×1
many-to-many ×1
orm ×1
php ×1
rails-admin ×1