MySQL/Rails中的歧义列查找方法 [英] Ambiguous column in MySQL/Rails find method

查看:97
本文介绍了MySQL/Rails中的歧义列查找方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到此错误

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中的歧义列查找方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆