Rails 多态查询
为什么它
@discussions = Discussion.where(:discussable => @discussable)
不起作用。但这有效:
@discussions = Discussion.where(:discussable_id => @discussable.id, :discussable_type => @discussable.class.to_s)
谢谢。
Why is it that
@discussions = Discussion.where(:discussable => @discussable)
doesn't work. yet this work:
@discussions = Discussion.where(:discussable_id => @discussable.id, :discussable_type => @discussable.class.to_s)
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来修复此问题的拉取请求已在 1 个月前合并到 master 中:
https://github.com/rails/rails/ pull/7273
不幸的是,它只会在 Rails 4 中发布:(
Looks like pull request which fixes this issue was merged into master 1 month ago:
https://github.com/rails/rails/pull/7273
Unfortunately, it will be only released in Rails 4 :(
因为在第一个中,您传递的是完整的模型实例。该属性必须是 id(整数)并且您传递一个对象。第二个没问题,因为你传递了 id(正确)。
Because in the first one you are passing a complete model instance. The attribute must be an id(integer) and you pass an object. The second one is ok, because you pass the id(correctly).