Mysql 在rails 4中使用子查询

Mysql 在rails 4中使用子查询,mysql,ruby-on-rails,ruby-on-rails-4,Mysql,Ruby On Rails,Ruby On Rails 4,我有以下两个子查询: @sub= ShareDetail.where(:dump_date_id=>2,:file_from_id=>1).select('TotalShares AS previous').group('previous') @sub1= ShareDetail.where(:dump_date_id=>1,:file_from_id=>1).select('TotalShares AS current').group('current

我有以下两个子查询:

    @sub= ShareDetail.where(:dump_date_id=>2,:file_from_id=>1).select('TotalShares AS previous').group('previous')

    @sub1= ShareDetail.where(:dump_date_id=>1,:file_from_id=>1).select('TotalShares AS current').group('current')
  class ShareHolder < ActiveRecord::Base 
  has_many :share_details,:dependent=>:destroy  
  end
在型号中:

    @sub= ShareDetail.where(:dump_date_id=>2,:file_from_id=>1).select('TotalShares AS previous').group('previous')

    @sub1= ShareDetail.where(:dump_date_id=>1,:file_from_id=>1).select('TotalShares AS current').group('current')
  class ShareHolder < ActiveRecord::Base 
  has_many :share_details,:dependent=>:destroy  
  end
我不知道如何使用这些子查询。请帮帮我。

你试过了吗

更新

class ShareHolder < ActiveRecord::Base 
  has_many :share_details,:dependent=>:destroy
end

class ShareDetail < ActiveRecord::Base
  scope :sub, -> { where(:dump_date_id=>2,:file_from_id=>1).select('TotalShares AS previous').group('previous') }
  scope :sub1, -> { where(:dump_date_id=>1,:file_from_id=>1).select('TotalShares AS current').group('current') }
end

获取类似“Mysql2::error:Unknown column”“previous”的错误
class ShareHolder < ActiveRecord::Base 
  has_many :share_details,:dependent=>:destroy
end

class ShareDetail < ActiveRecord::Base
  scope :sub, -> { where(:dump_date_id=>2,:file_from_id=>1).select('TotalShares AS previous').group('previous') }
  scope :sub1, -> { where(:dump_date_id=>1,:file_from_id=>1).select('TotalShares AS current').group('current') }
end
@shareDetails= ShareHolder.find_by(client_id: session[:shareinvestor_user_id]).share_details.sub