Использование строки:
@post = Post.friendly.where!(:board_id=>params[:board_id]).find(params[:id])
отделить сообщения :board_id
возвращает 0
, когда URL является /board1/thread/1/
, но возвращает правильное значение 1
когда URL является /1/thread/1/
freindly_id возвращает 0 для имени URL
Как я могу получить правильное значение либо 1
, либо, возможно, доску платы board1
?
Edit:
class Board < ActiveRecord::Base
has_many :posts
has_many :replies, through: :posts
include FriendlyId
friendly_id :name, use: :slugged
accepts_nested_attributes_for :posts, :replies
end
class Post < ActiveRecord::Base
belongs_to :board
has_many :replies, dependent: :destroy
accepts_nested_attributes_for :replies
include FriendlyId
friendly_id :pid, :use => :scoped, :scope => :id
after_create :set_pid
def set_pid
post_max = self.board.posts.maximum(:pid)
reply_max = self.board.replies.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
Можете ли вы показать нам, как вы настраиваете дружественный идентификатор для обеих моделей Post и Board? – pgaspar
@pgaspar обновил сообщение, чтобы включить обе модели – aidiah