我有3个控制器/型号
routes.rb
:
resources :boards do
resources :posts do
resources :replies
目前我为ID :pid
设置了一个单独的列,并为每个帖子/回复分配了一个有罪的PID。
在models/reply.rb
:
class Post < ActiveRecord::Base
belongs_to :board
has_many :replies, dependent: :destroy
has_one :board
accepts_nested_attributes_for :replies
include FriendlyId
friendly_id :pid, use: :slugged
after_create :set_pid
def set_pid
post_max = Post.maximum(:pid)
reply_max = Reply.maximum(:pid)
if post_max.to_i < reply_max.to_i
self.update_attributes(:pid => reply_max.to_i + 1)
else
self.update_attributes(:pid => post_max.to_i + 1)
end
end
end
这有效,但只有有一块板。如何分离每块板的PID?
答案 0 :(得分:0)
您必须使用范围:
post_max = self.board.posts.maximum(:pid)