我在 Sinatra 应用程序中使用 Pony 和 Sequel 时遇到了问题。没有 Pony 一切都很好,但只需要 Pony 续集的关联中断。这是我的博客模型:
class Post < Sequel::Model
one_to_many :comments, :order => :date.asc(), :conditions => {:approved => 1}
set_schema do
primary_key :id
varchar :title
varchar :text
varchar :category
varchar :status
datetime :date
varchar :link
end
end
class Comment < Sequel::Model
plugin :validation_helpers
many_to_one :posts
attr_accessor :ip, :user_agent, :referrer, :permalink
set_schema do
primary_key :id
integer :post_id
varchar :author
varchar :comment
DateTime :date
varchar :email
varchar :url
varchar :approved
end
然后我在一条路线中这样称呼他们
post '/:link' do
@post = Post[:link=>params[:link]]
params[:comment].merge!( {
:ip => request.ip.to_s,
:user_agent => request.env['HTTP_USER_AGENT'].to_s,
:referrer => request.env['REFERER'].to_s,
:permalink => request.env['REFERER'].to_s
} )
begin
@comment = Comment.create params[:comment]
@post.add_comment @comment
rescue
@message = $!
end
@title = @post.title
haml :posts
end
我什至不必在某个地方打电话给小马,只是要求它@post.add_comment @comment
失败。它说
NoMethodError - undefined method `_add_comments' for #<Post:0x102b09890>:
/Library/Ruby/Gems/1.8/gems/sequel-3.21.0/lib/sequel/model/associations.rb:1078:in `send'
/Library/Ruby/Gems/1.8/gems/sequel-3.21.0/lib/sequel/model/associations.rb:1078:in `add_associated_object'
/Library/Ruby/Gems/1.8/gems/sequel-3.21.0/lib/sequel/model/associations.rb:743:in `add_comment'
在我看来与发送冲突?我什至不知道如何开始调试它。