如何更改标准的CRUD动作映射?

时间:2013-10-20 08:46:49

标签: ruby-on-rails

我有帖子和评论,它们有一对一的关系,就像这样映射

  resources :posts do
    resources :comments
  end

This standard mapping使它们依赖于列:id是它们的主键,我希望它们是相同的,除非它们依赖于另一个表列:num(因为我无法自由更改主键而且它是自动的 - 无论如何增加),如何更改映射以实现它?

1 个答案:

答案 0 :(得分:1)

您可以使用其他方法查找comments控制器。例如:

class CommentsController < ApplicationController
  def index
    @post = Posts.find_by_num(params[:post_id])
    @comments = @post.comments
  end
end