我有帖子和评论,它们有一对一的关系,就像这样映射
resources :posts do
resources :comments
end
This standard mapping使它们依赖于列:id是它们的主键,我希望它们是相同的,除非它们依赖于另一个表列:num(因为我无法自由更改主键而且它是自动的 - 无论如何增加),如何更改映射以实现它?
答案 0 :(得分:1)
您可以使用其他方法查找comments
控制器。例如:
class CommentsController < ApplicationController
def index
@post = Posts.find_by_num(params[:post_id])
@comments = @post.comments
end
end