MySQL/Rails 查找方法中的列不明确
我收到这个错误
Mysql::错误:字段列表中的列“id”不明确
: self.prompts.find(:all, :select => 'id')
模型是使用 has_many :through 关联调用,因此 MySQL 抱怨有多个“id”列,因为使用的所有 3 个表都有一个“id”列。
我查了一下这个并了解 SQL 端出了什么问题,但不知道如何在 ActiveRecord find 方法中解决它,并且我对自己的 SQL 能力没有信心尝试滚动自己的 SQL 查询。有没有办法将 find 方法调整为可以正常运行的方法?
编辑
以下是相关的 Actor 模型代码:
class Actor < ActiveRecord::Base
has_many :acts, :dependent => :destroy
has_many :decisions, :through => :acts, :order => 'created_at'
has_many :prompts, :through => :decisions, :order => 'id'
I'm getting this error
Mysql::Error: Column 'id' in field list is ambiguous
when using a find method like such: self.prompts.find(:all, :select => 'id')
The models are being called using a has_many :through association, so MySQL complains that there are multiple 'id' columns, since all 3 tables being used have an 'id' column.
I looked this up and understand what is going wrong on the SQL end, but don't know how to resolve it in the ActiveRecord find method, and I'm not confident in my SQL abilities to try rolling my own SQL query. Is there a way to massage the find method into something that'll play well?
edit
Here is the relevant Actor model code:
class Actor < ActiveRecord::Base
has_many :acts, :dependent => :destroy
has_many :decisions, :through => :acts, :order => 'created_at'
has_many :prompts, :through => :decisions, :order => 'id'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要更明确地了解您要选择哪个 id。例如:
You need to be more explicit about which id you want to select. For example: