康康:“可以”方法再次获取记录,即使它们已经获取
我正在尝试传递这样的对象
<% if( can? :change, comment.karma, comment.user_id)%>
#blah blah blah
<% end %>
,在我的能力.rb 中,我有以下内容:
can :change, Karma do |karma, owner_id|
!karma.changers.map(&:user_id).include? user.id and owner_id != user.id
end
每次刷新页面时,cancan 都会触发一个查询(来自ability.rb
)以获取业力,然后该业力的改变者,即使我已经获取了它(使用 :include => { :karma => :changers }
选项在控制器中进行查询)。
添加:
我猜测这是因为 karma 存储了指向 :include 对象(而不是对象本身)的链接,当我将其传递给方法时,该链接实际上被破坏了。所以我现在正在考虑连载......
有什么想法吗?
I'm trying to pass an object like that
<% if( can? :change, comment.karma, comment.user_id)%>
#blah blah blah
<% end %>
and in my ability.rb i have the following:
can :change, Karma do |karma, owner_id|
!karma.changers.map(&:user_id).include? user.id and owner_id != user.id
end
And every time I refresh the page cancan fires a query (from ability.rb
) to get karma and then changers for that karma, even through I have already fetched it (using :include => { :karma => :changers }
option for query in controller).
Added:
I have a guess that it's happening because karma is storing a link to :include objects (not objects itself), which is actually get broken when I pass it to method. So I'm thinking about serialization now..
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在模型中执行查询时,请尝试使用此语法
@comments = Comment.where(:foo_id => Bar.id).includes(:karma => :changers)
并查看是否问题仍然存在。
作为参考,http://m.onkey.org/active-record-query-interface< /a> 详细介绍了新的 Rails 3 活动记录查询语法。
When you do the query in the model try using this syntax
@comments = Comment.where(:foo_id => Bar.id).includes(:karma => :changers)
and see if the problem persists.
For reference, http://m.onkey.org/active-record-query-interface has a great rundown of the new rails 3 active record querying syntax.