rails on rails上的ruby不执行模型方法

时间:2012-09-13 14:20:18

标签: ruby-on-rails methods model controller execute

controller.rb

if @object.save
    Post.method_name(@object)
end

post.rb

def self.method_name(object)
    post_id = 1
    p = Post.new
    p.post_id = post_id
    p.save
end

当执行Post.method_name(@object)时,即使我看不到任何错误,我也无法在表中看到新帖子...

1 个答案:

答案 0 :(得分:0)

解决方案:

我收到以下错误

Validation failed: Post type is not included in the list

当我使用p.save!

归因于validates :post_type, :presence => true, :inclusion=> { :in => @allowed_post_types }

所以当我添加我使用的帖子类型@allowed_post_types

的数组时

问题已解决