Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.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-on-rails-4/2.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 Get从ActiveRecord::Relatiton获得了很多_Ruby On Rails_Ruby On Rails 4_Activerecord_Rails Activerecord - Fatal编程技术网

Ruby on rails Get从ActiveRecord::Relatiton获得了很多

Ruby on rails Get从ActiveRecord::Relatiton获得了很多,ruby-on-rails,ruby-on-rails-4,activerecord,rails-activerecord,Ruby On Rails,Ruby On Rails 4,Activerecord,Rails Activerecord,是否可以从ActiveRecord::关系中获取多条记录 i、 e.Book.where(虚构:真)。页面与Book.where(虚构:真)相反。收集{Book}Book.pages} 理想情况下,我希望能够使用一个ActiveRecord查询来获取所有记录,这样我就不必在内存中构建数组,也不必让代码变得更干净,尤其是当关系具有多个级别时(即Book.where(虚构:true).pages.words如下所示 Book.where(fiction: true).collect { |book|

是否可以从ActiveRecord::关系中获取多条记录

i、 e.
Book.where(虚构:真)。页面
Book.where(虚构:真)相反。收集{Book}Book.pages}

理想情况下,我希望能够使用一个ActiveRecord查询来获取所有记录,这样我就不必在内存中构建数组,也不必让代码变得更干净,尤其是当关系具有多个级别时(即
Book.where(虚构:true).pages.words

如下所示

Book.where(fiction: true).collect { |book| book.pages }
可以写为:

Page.joins(:book).where("books.fiction = ?", true)
类似的方式:

Word.joins(page: :book).where("books.fiction = ?", true)
那么下面,

Book.where(fiction: true).collect { |book| book.pages }
可以写为:

Page.joins(:book).where("books.fiction = ?", true)
类似的方式:

Word.joins(page: :book).where("books.fiction = ?", true)

您可以在父对象(比如说它是
)和
页面
之间创建单独的
has\u many
关系,并使用选项

class Page
  belongs_to :book
end

class Book
  belongs_to :library
  has_many :pages
end

class Library
  has_many :books
  has_many :pages, through: :books
  has_many :fiction_books, -> { where(fiction: true) }, class_name: 'Book', foreign_key: 'library_id'
  has_many :fiction_pages, through: :fiction_books, source: :pages
end

Library.first.fiction_pages
我会注意到,父对象将帮助您使用过于复杂的逻辑构建正确的体系结构,因此,您只需编写当前库。小说页面,而不是
Book.where(虚构:true).pages

如果应用程序的逻辑不需要这样的父对象,您可以通过单独的辅助对象(可放置在
ApplicationController
中)来伪造它:

class ApplicationController
  helper_method :current_library

  protected

  def current_library
    @current_library ||= Library.first
  end

end

在这种情况下,所有AR对象都应该通过库方法或库的子对象来获取。

您可以在父对象(假设它是
)和
页面
之间创建单独的
关系,并选择

class Page
  belongs_to :book
end

class Book
  belongs_to :library
  has_many :pages
end

class Library
  has_many :books
  has_many :pages, through: :books
  has_many :fiction_books, -> { where(fiction: true) }, class_name: 'Book', foreign_key: 'library_id'
  has_many :fiction_pages, through: :fiction_books, source: :pages
end

Library.first.fiction_pages
我会注意到,父对象将帮助您使用过于复杂的逻辑构建正确的体系结构,因此,您只需编写当前库。小说页面,而不是
Book.where(虚构:true).pages

如果应用程序的逻辑不需要这样的父对象,您可以通过单独的辅助对象(可放置在
ApplicationController
中)来伪造它:

class ApplicationController
  helper_method :current_library

  protected

  def current_library
    @current_library ||= Library.first
  end

end

在这种情况下,所有AR对象都应该通过library方法或通过childs of library获取。

如果我已经将初始集合作为一个关系,比如在视图中,有没有办法这样做?例如,我有
@books
,并且想要获取这些书籍的所有单词。@Asherlc是的。使用
includes
方法,比如
Book.includes(:页)。其中(虚构:真实)
。这是一个单独的查询,它解决了
N+1
问题。@Asherlc read抱歉,我认为我没有很好地解释我的问题。假设我只有一个包含ActiveRecord::Relation的变量,并且没有访问原始查询参数的权限。因此,我想以
@books
和fin的一个实例变量为例d所有关联的页面/单词,仅给定预构建的ActiveRecord::Relation。这可能吗?如果我已经将初始集合作为一个关系(如视图中的关系),有没有办法做到这一点?例如,我有
@books
,并希望获得这些书籍的所有单词。@Asherlc是的。使用
\includes
方法,如
Book.includes(:pages).何处(虚构:真实)
。这是一个单独的查询,它解决了
N+1
问题。@Asherlc read抱歉,我认为我没有很好地解释我的问题。假设我只有一个包含ActiveRecord::Relation的变量,并且没有访问原始查询参数的权限。因此,我想以
@books
和fin的一个实例变量为例d所有关联的页面/单词,只给定预构建的ActiveRecord::Relation。这可能吗?