我遇到此错误
Mysql :: Error:字段列表中的列"id"不明确
Mysql::Error: Column 'id' in field list is ambiguous
使用类似以下的查找方法时:self.prompts.find(:all, :select => 'id')
when using a find method like such: self.prompts.find(:all, :select => 'id')
正在使用has_many:through关联来调用模型,因此MySQL抱怨存在多个"id"列,因为使用的所有3个表都有一个"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.
我检查了一下,了解了SQL端出了什么问题,但是不知道如何在ActiveRecord查找方法中解决它,并且我对自己的SQL能力不满意,无法尝试滚动自己的SQL查询.有没有一种方法可以将find方法按摩到可以很好发挥作用的东西上?
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?
编辑
以下是相关的Actor模型代码:
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'推荐答案
您需要更明确地了解要选择的ID.例如:
You need to be more explicit about which id you want to select. For example:
self.prompts.find(:all, :select => 'prompts.id') #prompts is the table name更多推荐
MySQL/Rails中的歧义列查找方法
发布评论