Ruby on rails 独立邮政ID
我有3个控制器/型号Ruby on rails 独立邮政ID,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有3个控制器/型号 routes.rb: resources :boards do resources :posts do resources :replies class Post < ActiveRecord::Base belongs_to :board has_many :replies, dependent: :destroy has_one :board accepts_nested_attributes_for :replies
routes.rb
:
resources :boards do
resources :posts do
resources :replies
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
目前,我为ID:pid单独设置了一列,并为每个帖子/回复分配了一个有罪的pid
在模型/reply.rb
中:
resources :boards do
resources :posts do
resources :replies
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
class Postreply_max.to_i+1)
其他的
self.update_属性(:pid=>post_max.to_i+1)
结束
结束
结束
这是可行的,但只有在有一块板的情况下。如何将每个板的PID分开?您必须使用范围:
post_max = self.board.posts.maximum(:pid)
您必须使用范围:
post_max = self.board.posts.maximum(:pid)
这返回错误:没有这样的列:boards.post\u id:从“boards”中选择“boards”。*其中“boards”。“post\u id”=?限制1
如果定义了关系,但列不正确,则架构中存在错误。在你的问题中,它说属于:board
,这意味着应该可以工作,但是有一个可能是个错误。@aidiah值得为此打开另一个问题,一次一件。这返回错误:没有这样的列:boards.post\u id:从“boards”中的“boards”中选择“boards”。“post\u id”=?限制1
如果定义了关系,但列不正确,则架构中存在错误。在你的问题中,它说属于:board
,这意味着它应该工作,但是有一个可能是个错误。@aidiah值得为此打开另一个问题,一次一件事。