freindly_id returns 0 表示 url

freindly_id returns 0 for named url

使用线路:
@post = Post.friendly.where!(:board_id=>params[:board_id]).find(params[:id])
将帖子 :board_id returns 和 0 分隔开,当 url 是 /board1/thread/1/ 时,但是 returns 是 1 的正确值url 是 /1/thread/1/

如何获得 1board1 的正确值?

编辑:

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

根据 documentation 这样的内容应该适合你:

@post = Board.friendly.find(params[:board_id]).posts.friendly.find(params[:id])

请注意,这将执行两个查询 - 一个用于查找版块,另一个用于在该版块中查找 post。

有多种方法可以在单个查询中执行此操作(例如使用左外连接),尽管它们会引入与您在 Board 模型上使用的友好 ID 配置的耦合。示例:

Post.includes(:board).where('boards.slug' => params[:board_id]).friendly.find(params[:id])