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 如何连接两个活动记录结果以返回可进一步筛选的新结果?_Ruby On Rails_Ruby_Ruby On Rails 3_Activerecord_Model - Fatal编程技术网

Ruby on rails 如何连接两个活动记录结果以返回可进一步筛选的新结果?

Ruby on rails 如何连接两个活动记录结果以返回可进一步筛选的新结果?,ruby-on-rails,ruby,ruby-on-rails-3,activerecord,model,Ruby On Rails,Ruby,Ruby On Rails 3,Activerecord,Model,想象一下这个场景 #models/user.rb class User < ActiveRecord::Base has_many :accounts, :conditions => { :active => 1 } end #models/account.rb class Account < ActiveRecord::Base belongs_to :user def public_accounts Account.all

想象一下这个场景

#models/user.rb
class User < ActiveRecord::Base
    has_many :accounts, :conditions => { :active => 1 }
end

#models/account.rb
class Account < ActiveRecord::Base
    belongs_to :user

    def public_accounts
        Account.all :conditions => { public => true }
    end
end
想法

更新#1: 范围不适用于我的场景。我简化了我的场景,试图表达我的观点。如上所述,我需要一种方法来组合两个活动记录结果,并保留在控制器中进一步过滤它们的能力

所以问题是,“为什么?”原因是,我试图将两组记录组合成一个完整的集合,其中一个集合根本与用户无关


我对上述场景进行了重构,以尝试在不过于复杂的情况下展示更精确的示例。

这可能是一个很好的使用场景

您可以在帐户模型中定义活动和非活动作用域,然后使用以下内容:

User.accounts
User.accounts.active
User.accounts.inactive
您甚至可以将作用域链接在一起,以便执行以下操作:

User.accounts.active.paid_up
jklina是正确的,在这种情况下最好使用示波器。作用域提供了一种甜美的语法,并且更具可读性。我将详细介绍设置:

class User < AR::Base
  has_many :accounts
end

class Account < AR::Base
  belongs_to :user

  scope :active, where(:active => true)
  scope :inactive, where(:active => false)
end

让我们看看链中的返回值:

User.find(params[:user_id]) # returns an instance of User class
User.find(params[:user_id]).all_accounts # returns an array
Array类没有名为
all
的实例方法,这就是您看到此错误的原因。这不是一个bug

你为什么不试试这个:

class User
  has_many :accounts, :conditions => { :active => 1 }
  has_many :all_accounts :conditions => ["(active = ? OR public = ?)", 
                           true, true]
end
现在您可以:

User.find(params[:user_id]).all_accounts.all(:limit => 10, :offset => 2)

您尝试访问两个不同的表,并将限制/偏移作为一个组合的并集应用于它们。除非在SQL层而不是ActiveRecord层逻辑地组合它们,否则这不会发生


听起来像是在写SQL,可能是使用一个
联合
,然后使用
find\u by\u SQL
可能是你最好的选择。

当你添加的方法是
all\u accounts
时,为什么你不能使用
all
?Kandaboggu,试试看。我的问题过去是,现在仍然是,如何连接到活动记录结果。因为这个问题没有得到回答,所以我对我的场景进行了修改,试图更接近我“相信”自己需要这样做的原因。谢谢,但如果可能的话,我会使用示波器。更新我的答案。看一看。那不行。它不是一个活跃的或公开的。它是帐号=用户,会计或公众。我甚至没有考虑在用户中做什么。这是一个好主意,不幸的是,“公众是真实的”看起来不起作用。当我试着这么做的时候,这是一个错误,我会试着回去做。我真的了解了自我。执行此操作后对方法的声明。重要的是,在Rails 4中,作用域需要符号和lambda,il Rails 3也支持这一点,所以我建议使用
scope:active,->{where(active:true)}
来保持它的工作状态(并且它与更复杂的作用域更为一致)。看起来可能就是这样。我讨厌那样做。。。更确切地说,在模型中有逻辑。然后,您可能能够编写一个SQL视图来封装您的查询,并使其看起来像是您的模型可以执行的一个简单查询
User.find(params[:user_id]) # returns an instance of User class
User.find(params[:user_id]).all_accounts # returns an array
class User
  has_many :accounts, :conditions => { :active => 1 }
  has_many :all_accounts :conditions => ["(active = ? OR public = ?)", 
                           true, true]
end
User.find(params[:user_id]).all_accounts.all(:limit => 10, :offset => 2)