Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails提取包含数据的每个属性_Ruby On Rails_Ruby_Ruby On Rails 4_Activerecord - Fatal编程技术网

Ruby on rails Rails提取包含数据的每个属性

Ruby on rails Rails提取包含数据的每个属性,ruby-on-rails,ruby,ruby-on-rails-4,activerecord,Ruby On Rails,Ruby,Ruby On Rails 4,Activerecord,我有一个conversations控制器,用于为seller类型的用户提取所有对话 卖家可以拥有产品,买家可以购买,产品可能是免费的。对于对话列表,每个买家被标记为客户或潜在客户,具体取决于以下因素: 买家购买了付费产品或订阅了产品=>客户 else=>lead 我的转换\u控制器是: @conversation = Conversation.includes(:messages) .get_coach_conversations(@sel

我有一个
conversations
控制器,用于为
seller
类型的用户提取所有对话

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

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

else
=>
lead

我的
转换\u控制器是:

@conversation = Conversation.includes(:messages)
                            .get_coach_conversations(@seller)
我的
对话
模型具有以下方法:

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
然后在我的
jbuilder
中,对于
卖家/对话
索引
方法,我有以下检查来检查
买家
客户
还是
潜在客户

coach/conversations/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)
我想这可能是一个缓慢的请求,因为我正在为检查提取大量内容,但是
join
似乎在提取ActiveRecord请求中包含的每个
模型的所有属性。想知道是否有更好的方法来检查这一点,而不需要拉所有的数据

对于my Include,已包含以下信息的型号:

买家
->
用户
通过
买家/卖家
模型连接到
对话

采购
->检查
买家
是否为
客户/潜在客户


用户
->
买家
名称
显示图片
无论您做什么,您都应该将其移出视图并移至买家可能使用的方法。但是从我们可以看出,如果它只是一个活动记录关系,就不必构建ruby对象。在买方上创建范围并传入
@seller

c.buyer.purchases
       .where(seller: @seller)
       .where('subscription = ? OR product_price > ?',true, 0)
       .exists?
移动到模型,可能购买

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

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

为什么不将买家是否是客户/潜在客户存储为客户表中的布尔值?@lacostenycoder,因为买家可能是特定
卖家的
客户
,但可能是不同
卖家的
潜在客户
,这取决于
买家
是否从特定
卖家
购买了任何付费产品对于建模方法,大多数模型逻辑都尽可能在视图中保留一点逻辑,尤其是在迭代时。如果不了解您的模型及其关系,很难提供更具体的帮助。谢谢您的帮助。我按照你的建议把它移到了一个范围内,但我仍在寻找一种方法来提高效率。具体地说,我不确定是不是
连接导致它加载的数据超出了需要
.join
只是执行sql连接,因此它不应该导致任何性能问题。但是您可能想使用来检查N+1查询等。对于内存问题,请检查,还有许多其他问题