Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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
Mysql 选择所有具有非空关联记录的记录_Mysql_Postgresql_Activerecord_Ruby On Rails 4_Will Paginate - Fatal编程技术网

Mysql 选择所有具有非空关联记录的记录

Mysql 选择所有具有非空关联记录的记录,mysql,postgresql,activerecord,ruby-on-rails-4,will-paginate,Mysql,Postgresql,Activerecord,Ruby On Rails 4,Will Paginate,我有两种型号 class User < ActiveRecord::Base has_many :deals end 我知道这很简单,但请帮助我,因为我需要它来使用will_paginate进行分页。有很多方法可以实现这一点,但我的首选方法是在user.rb中写入作用域,如下所示 class User < ActiveRecord::Base has_many :deals scope :with_deals, lambda{ joins(:deals).un

我有两种型号

class User < ActiveRecord::Base
  has_many :deals
end

我知道这很简单,但请帮助我,因为我需要它来使用will_paginate进行分页。

有很多方法可以实现这一点,但我的首选方法是在user.rb中写入作用域,如下所示

class User < ActiveRecord::Base
  has_many :deals

  scope :with_deals, lambda{
    joins(:deals).uniq
  }
}

或者你需要的任何东西。

我知道这不是一个好方法,但它对我来说很有效,只需添加我的答案,也许它对任何人都有帮助

restaurants_ids = User.all.collect{|u| u.id if (u.role == "restaurant" && !u.deals.empty?)}
@restaurants = User.where(:id=>restaurants_ids).paginate(:page => params[:page], :per_page=>1)

当我有多个交易的用户时,正在为用户获取空数组。范围正常,是否确实有一些交易具有正确的用户id属性?请记住,paginate部分只是一个示例…我可以使用include或join的其他方式吗?
class User < ActiveRecord::Base
  has_many :deals

  scope :with_deals, lambda{
    joins(:deals).uniq
  }
}
User.with_deals.paginate(:page => params[:page])
restaurants_ids = User.all.collect{|u| u.id if (u.role == "restaurant" && !u.deals.empty?)}
@restaurants = User.where(:id=>restaurants_ids).paginate(:page => params[:page], :per_page=>1)