如何改进has_one关联activerecord查询

时间:2022-10-04 15:46:43

I've a biography model, and it has one to one association with lifestyle model, one with education model, and one to one with location model.

我有一个传记模型,它和生活方式模型,教育模型,还有位置模型。

When application loads I need to get all of this information. I'm doing this by:

当应用程序加载时,我需要得到所有这些信息。我是这样做的:

biography = current_user.biography
lifestyle = biography.lifestyle
education = biography.education
location = biography.location

result = { "biography" => biography, "lifestyle" => lifestyle, "education" => education, "location" => location}

And then sending the json result back:

然后返回json结果:

render :json => result

So I'm executing total of four queries for this get. Is there a way to issue less queries and get the same result back?

我为这个get执行了4个查询。是否有一种方法可以发出较少的查询并获得相同的结果?

I've tried using joins, but it is only returning columns from one table. n+1 won't really help here as I'm not looping over the results. Also, includes hasn't given me the desired results also.

我尝试过使用join,但它只是从一个表返回列。n+1在这里没用,因为我没有对结果进行循环。还有,包括没有给我想要的结果。

Is there a better alternative?

还有更好的选择吗?

Here are some of the associations:

以下是一些联想:

class Lifestyle < ActiveRecord::Base
   belongs_to :biography
end

class Biography < ActiveRecord::Base
   has_one :lifestyle, dependent: :destroy
   has_one :education, dependent: :destroy
   has_one :location, dependent: :destroy
end

2 个解决方案

#1


4  

biography = Biography.where(user_id: current_user.id).eager_load(:lifestyle, :education, :location).first

传记=传记。在哪里(user_id:current_user.id)。eager_load(:生活方式,:教育:位置)当代

:)

:)

#2


2  

This seems to me like a case for as_json. You could overwrite the default method in your Biography model to include the attributes you need for the biography, the associations you want to include, and the attributes for each of those associations (see http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json). Then, to avoid n+1s, you could do something like:

在我看来,这是as_json的一个例子。可以覆盖传记模型中的默认方法,以包含传记所需的属性、要包含的关联以及每个关联的属性(参见http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json)。为了避免n+1,你可以这样做:

biography = Biography.includes(:lifestyle, :education, :location).first

render json: biography.as_json

#1


4  

biography = Biography.where(user_id: current_user.id).eager_load(:lifestyle, :education, :location).first

传记=传记。在哪里(user_id:current_user.id)。eager_load(:生活方式,:教育:位置)当代

:)

:)

#2


2  

This seems to me like a case for as_json. You could overwrite the default method in your Biography model to include the attributes you need for the biography, the associations you want to include, and the attributes for each of those associations (see http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json). Then, to avoid n+1s, you could do something like:

在我看来,这是as_json的一个例子。可以覆盖传记模型中的默认方法,以包含传记所需的属性、要包含的关联以及每个关联的属性(参见http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json)。为了避免n+1,你可以这样做:

biography = Biography.includes(:lifestyle, :education, :location).first

render json: biography.as_json