【发布时间】:2012-08-21 03:17:07
【问题描述】:
我有一个简单的授权功能,但我很难正确实施。每个帖子在创建时都会发出一个 :key_code ,并且用户(未会话)可以键入代码并删除他们的帖子。问题是,无论他们为 key_code 键入什么,对象都会被传递。
为什么总是返回 0?
field :key_code, type: Integer
def self.auth(id, key_code)
post = Post.where(:id => id).first
puts key_code #for testing:
puts post.key_code #for testing:
if post.key_code == key_code
return 1
else
return 0
end
end
控制台测试:
1.9.3p194 :001 > a = Post.auth('5032f3254ff9fcf10100001b','42745590875')
42745590875
42745590875
=> 0
【问题讨论】:
-
您是使用
post_id还是简单地使用id作为posts 表中的主键? -
是的。它不是嵌套资源,只是 mongoid
标签: ruby-on-rails ruby-on-rails-3 class mongoid