Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.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 如何在嵌套资源的控制器中为show操作构建活动的Recond查询?_Ruby On Rails_Ruby On Rails 3_Activerecord_Nested Resources - Fatal编程技术网

Ruby on rails 如何在嵌套资源的控制器中为show操作构建活动的Recond查询?

Ruby on rails 如何在嵌套资源的控制器中为show操作构建活动的Recond查询?,ruby-on-rails,ruby-on-rails-3,activerecord,nested-resources,Ruby On Rails,Ruby On Rails 3,Activerecord,Nested Resources,我有两种模式:书和章,书有很多章。 我设置了如下路线: match 'book/:book_title/:chapter/:chapter_title' => 'chapter#show', :as => "chapter" 而且,授权给章节控制员,动作秀效果很好 我现在的问题是通过这本书检索show controller中的那一章。 如果查询的标识符不是主键,如何实现这一点 谢谢 您可以通过以下书籍加载章节: @book = Book.find_by_title(params[:

我有两种模式:书和章,书有很多章。 我设置了如下路线:

match 'book/:book_title/:chapter/:chapter_title' => 'chapter#show', :as => "chapter"
而且,授权给章节控制员,动作秀效果很好

我现在的问题是通过这本书检索show controller中的那一章。 如果查询的标识符不是主键,如何实现这一点


谢谢

您可以通过以下书籍加载章节:

@book = Book.find_by_title(params[:book_title])
@chapter = @book.chapters.find_by_title(params[:chapter_title])

注意:find_by_*适用于该模型上的任何数据库属性。

您可以通过以下书籍加载章节:

@book = Book.find_by_title(params[:book_title])
@chapter = @book.chapters.find_by_title(params[:chapter_title])

注意:find_by_*适用于该模型上的任何数据库属性。

我花了一段时间试图找出它不起作用的原因,结果发现问题出在父模型中使用的默认_范围内。我像
default\u scope那样编写它,其中(:attribute=>:value)
使用find\u by.*导致了错误的结果,我将其更改为
default\u scope:conditions=>{:attribute=>:value}
,现在它工作正常。谢谢我花了一段时间试图弄清楚它为什么不起作用,结果发现问题出在我在父模型中使用的默认\u范围内。我像
default\u scope那样编写它,其中(:attribute=>:value)
使用find\u by.*导致了错误的结果,我将其更改为
default\u scope:conditions=>{:attribute=>:value}
,现在它工作正常。谢谢