Rails提取所包含数据的每个属性

时间:2022-08-16 09:50:47

I have a conversations controller which is used to pull all the conversations for a user of type seller.

我有一个会话控制器,用于为卖家类型的用户提取所有会话。

Sellers can have products which can be purchased by buyers and the product may be free. For the list of conversations every buyer is tagged as a client or a lead depending on the following factors:

卖家可以拥有买家可以购买的产品,产品可以免费。对于会话列表,每个买家都被标记为客户或潜在客户,具体取决于以下因素:

buyer purchased a paid product or subscribed to a product => client

买方购买了付费产品或订购了产品=>客户

else => lead

else =>领导

My converastions_controller is:

我的converastions_controller是:

@conversation = Conversation.includes(:messages)
                            .get_coach_conversations(@seller)

My Conversation model has the method:

我的Conversation模型有这样的方法:

def self.get_seller_conversations(seller)
  @conversations = seller.conversations
                         .includes(buyer: [:purchases, :user])
                         .joins(:messages)
                         .where(messages: {only_for_buyer: false})
                         .distinct
  new.sorted_conversations(@conversations)
end

def sorted_conversations(conversations)
  conversations.sort_by { |c| c.messages.last.created_at }
               .reverse
end

And then in my jbuilder for the seller/conversations index method I have the following check to check if the buyer is a client or a lead

然后在我的jbuilder中为卖家/对话索引方法我有以下检查以检查买家是客户还是领导

coach/conversations/index.json.jbuilder:

教练/会话/ index.json.jbuilder:

json.array!(@conversations.map do |c|
  ...
  ...
  already_client: c.buyer.purchases
                         .where(seller: @seller)
                         .where('subscription = ? OR product_price > ?',
                                 true, 0)
                         .exists?
end)

I think I can expect this to be a slow request because I'm pulling a lot of content for the check but that join seems to be pulling all the attributes of every model which is included in the ActiveRecord request. Wondering if there's a better way to check this without needing to pull all the data.

我想我可以期待这是一个缓慢的请求,因为我为检查提取了大量内容,但是这个连接似乎是拉动了ActiveRecord请求中包含的每个模型的所有属性。想知道是否有更好的方法来检查这一点,而无需提取所有数据。

For my includes the models have been included for the following information:

对于我的包含,已包含模型以获取以下信息:

buyer -> users are connected to conversations through buyer/seller model

买方 - >用户通过买方/卖方模式与对话相关联

purchases -> check if buyer is a client/lead

购买 - >检查买方是否是客户/主管

user -> buyers name and display picture

用户 - >买家姓名和显示图片

1 个解决方案

#1


1  

Whatever you do you should move this out of the view and to a method probably on buyer. But from what we can tell, this shouldn't have to build ruby objects if it's just an active record relation. Create a scope on buyer and pass in @seller

无论你做什么,你都应该将其从视图中移出,并将其移到买方的方法上。但是从我们可以看出,如果它只是一个活跃的记录关系,则不应该构建ruby对象。在买家上创建一个范围并传入@seller

c.buyer.purchases
       .where(seller: @seller)
       .where('subscription = ? OR product_price > ?',true, 0)
       .exists?

move to model, probably Purchase?

搬到模特,可能是购买?

class Purchase
  scope :already_client, -> (seller) {
    .where(seller: seller)
    .where('subscription = ? OR product_price > ?',true, 0)
    .exists?
  }
end

inside your json builder:

在你的json构建器中:

already_client: c.buyer.purchases.already_client(@seller)

#1


1  

Whatever you do you should move this out of the view and to a method probably on buyer. But from what we can tell, this shouldn't have to build ruby objects if it's just an active record relation. Create a scope on buyer and pass in @seller

无论你做什么,你都应该将其从视图中移出,并将其移到买方的方法上。但是从我们可以看出,如果它只是一个活跃的记录关系,则不应该构建ruby对象。在买家上创建一个范围并传入@seller

c.buyer.purchases
       .where(seller: @seller)
       .where('subscription = ? OR product_price > ?',true, 0)
       .exists?

move to model, probably Purchase?

搬到模特,可能是购买?

class Purchase
  scope :already_client, -> (seller) {
    .where(seller: seller)
    .where('subscription = ? OR product_price > ?',true, 0)
    .exists?
  }
end

inside your json builder:

在你的json构建器中:

already_client: c.buyer.purchases.already_client(@seller)