1
私は3のコントローラ/モデル別々のポストIDを
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
これは動作しますが、1枚のボードがある場合のみ。ボードごとにPIDをどのように分けることができますか?
を "ボード" を選択* "ボード" "ボード" FROM" post_idの。 "=? LIMIT 1' – aidiah
関係が定義されていて、右の列がない場合は、スキーマにエラーがあります。あなたの質問では 'belongs_to:board'と言います。これはうまくいくはずですが、' has_one'はおそらく間違いです。 – tadman
@aidiah一度にもう一つの質問を開く価値がある。 – tadman