Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.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 ActiveRecordRails查找没有组的页面_Ruby On Rails - Fatal编程技术网

Ruby on rails ActiveRecordRails查找没有组的页面

Ruby on rails ActiveRecordRails查找没有组的页面,ruby-on-rails,Ruby On Rails,我对以下情况有信心: 页面有许多组 现在我试图找到第一个没有activerecord组的页面。 我试过这样的方法: Page.find(:first, :joins => :groups, :select => 'DISTINCT `pages`.*') 当然,到目前为止运气不好。一个解决方案可能是为组添加计数器缓存 In Group.rb belongs_to :page, :counter_cache => true 然后,您需要创建一个迁移 def self.up

我对以下情况有信心: 页面有许多组

现在我试图找到第一个没有activerecord组的页面。 我试过这样的方法:

Page.find(:first, :joins => :groups, :select => 'DISTINCT `pages`.*')

当然,到目前为止运气不好。

一个解决方案可能是为组添加计数器缓存

In Group.rb

belongs_to :page, :counter_cache => true
然后,您需要创建一个迁移

def self.up
  add_column :pages, :groups_count, :integer, :default => 0

  Page.reset_column_information
  Page.find(:all).each do |p|
    Page.update_counters p.id, :groups_count => p.groups.length
  end
end

def self.down
  remove_column :pages, :groups_count
end
所以,现在你可以做:

Page.first(:conditions => { :groups_count => 0 })

一种解决方案是为组添加计数器缓存

In Group.rb

belongs_to :page, :counter_cache => true
然后,您需要创建一个迁移

def self.up
  add_column :pages, :groups_count, :integer, :default => 0

  Page.reset_column_information
  Page.find(:all).each do |p|
    Page.update_counters p.id, :groups_count => p.groups.length
  end
end

def self.down
  remove_column :pages, :groups_count
end
所以,现在你可以做:

Page.first(:conditions => { :groups_count => 0 })

与ActiveRecord相比,这更像是一个SQL问题。您需要的是联接这两个表,但选择联接表中未引用的表

Page.first(:include => :groups, :conditions => ["`#{Group.table_name}`.id IS NULL"]
# The opposite query would be
Page.first(:include => :groups, :conditions => ["`#{Group.table_name}`.id IS NOT NULL"]
# Note that this slightly different than just: 
Page.first(:include => :groups)
# This produces a IN query, rather than join.

另外
:联接
不起作用,因为它创建了一个
内部联接
,而不是本例中需要的
外部联接。

这更像是一个SQL问题,而不是ActiveRecord。您需要的是联接这两个表,但选择联接表中未引用的表

Page.first(:include => :groups, :conditions => ["`#{Group.table_name}`.id IS NULL"]
# The opposite query would be
Page.first(:include => :groups, :conditions => ["`#{Group.table_name}`.id IS NOT NULL"]
# Note that this slightly different than just: 
Page.first(:include => :groups)
# This produces a IN query, rather than join.
另外
:联接
不起作用,因为它创建了一个
内部联接
,而不是本例中需要的
外部联接。

类似于:

Page.find(:all, 
          :select => "pages.*, count(groups.page_id) group_count", 
          :joins => :groups, 
          :group => "pages.id having group_count = 0)
比如:

Page.find(:all, 
          :select => "pages.*, count(groups.page_id) group_count", 
          :joins => :groups, 
          :group => "pages.id having group_count = 0)

好的,可以。但我不太喜欢为这个简单的语句创建另一个迁移:(然后我就可以用sql来做了..但是thanxs!好的,是的,这会起作用。但是我真的不喜欢为这个简单的语句创建另一个迁移。:(然后我就可以用sql来做..但是thanxs!是的,这更好!)…thanxs!:这甚至更好!(我解决了所有页面循环的问题..效率不高;)。。thanxs!:D