【发布时间】:2016-03-21 21:46:47
【问题描述】:
我有一个 rails4 应用程序。我有下面的架构,并将添加post_replies 表,该表将belong_to :post_comment 和post_commentwill has_many :post_replies。评论下的回复将始终属于给定的评论。
我的问题是 post_replies 应该添加多少外键?我将始终仅在post index page 上显示它们,并且将使用format_js 添加新回复。 post_reply belongs_to post_comment 当然可以,但我应该同时使用 belongs_to :user 和 belongs_to :post 吗?
当前架构:
class User < ActiveRecord::Base
has_many :posts
has_many :post_comments, through: :posts
end
class Post < ActiveRecord::Base
has_many :post_comments
belongs_to :user
end
class PostComment < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
计划的架构:
class PostReply < ActiveRecord::Base
belongs_to :post_comment #this is needed for sure
belongs_to :post #do i need this?
belongs_To :user #and this?
end
路线:
#current:
resources :posts do
resources :post_comments, only: [:create, :update, :destroy], module: :posts
end
#and planning to add:
resources :post_comments, only: [] do
resources :post_repiles, only: [:create, :update, :destroy], module: :posts
end
【问题讨论】:
标签: ruby-on-rails activerecord foreign-keys schema relational-database