我正在尝试构建一个连接,如下所示:
UserLog.find(:all, :joins => " JOIN client_inspector ON user_log.COMPUTER_NAME = client_inspector.Retrieving_Hostname ", :select=> "DISTINCT user_log.COMPUTER_NAME, client_inspector.Retrieving_Hostname ")但是它给了我下面的错误->
ActiveRecord::StatementInvalid (Mysql2::Error: Table 'user_log.client_inspector' doesn't exist: SELECT DISTINCT user_log.COMPUTER_NAME, client_inspector.Retrieving_Hostname FROM `user_log` JOIN client_inspector ON user_log.COMPUTER_NAME = client_inspector.Retrieving_Hostname):
我理解MYSQL错误,但是为什么Rails要这样做呢?
我没有创建任何将这两个表关联在一起的模型。我想要做的是能够通过Ruby内置的默认列之外的不同列加入。
有没有不同的方法来做这件事?
编辑:我更新了我的实际表名。
发布于 2012-09-15 02:43:59
任何正在寻找这个问题的答案的人,只需使用它来获取您的数据->
@returned_fields = ActiveRecord::Base.connection.execute(sql)这不会返回"Model“对象,而只返回一个通用的MySQL::Result对象。使用此命令可获取数据->
@returned_fields.each这将为您提供一个数组数组,如so ->
[[1,2], [3,4], etc...]此外,请确保使用数据库名称对表进行限定,如->
SELECT foo FROM database.BAR as bar JOIN database.BLEZ as blez ON bar.COMPUTER_NAME = blez.Retrieving_Hostname我的问题原来是数据库的资格问题。因为我不符合条件,所以它在错误的数据库(我当前连接的数据库)中查找表。
发布于 2012-09-11 00:47:26
mysql请求有效。我认为你不需要在你的模型之间建立联系。
模型的表名是用复数名称创建的,您可以尝试这样做吗:
UserLog.find(:all, :joins => " JOIN client_inspectors ON user_logs.COMPUTER_NAME = client_inspectors.Retrieving_Hostname ", :select=> "DISTINCT user_logs.COMPUTER_NAME, client_inspectors.Retrieving_Hostname ")mysql查询中的每个表名都有一个"s“
编辑:使用表名更新
https://stackoverflow.com/questions/12355271
复制相似问题