引用列名 [英] Quote column name

查看:192
本文介绍了引用列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含2罕见列的表 - 喜欢

I have a table containing 2 "uncommon" columns - order and like.

表被复制的两个数据库。 - 的MySQL PostgreSQL的

The table are replicated on two databases - MySQL and PostgreSQL.

和我需要相同的应用程序连接到这两个数据库,并使用相同的查询两个:

And i need same app to connect to both databases and use same query on both:

PageModel.where("`like` >= ?", params[:liked])

这将工作在的MySQL 只。

如何让的ActiveRecord 引用的列名?

是这样的:

PageModel.where("%s >= ?" % quote_column_name(:like), params[:liked])

我发现了一个方法,该方法是没有用的,现在 - 它只是返回列名不带引号它

I found a method that is useless for now - it just returns the column name without quote it.

<一个href="http://www.rubydoc.info/docs/rails/3.2.8/ActiveRecord/ConnectionAdapters/Quoting:quote_column_name" rel="nofollow">http://www.rubydoc.info/docs/rails/3.2.8/ActiveRecord/ConnectionAdapters/Quoting:quote_column_name

也许这只是一个占位符,有,这是否另一种方法?

Perhaps it is just a placeholder and there are another method that does this?

推荐答案

这取决于从一个数据库适配器到另一个。所以基本上,每个适配器重写此方法与自己的定义。

This depends from from one database adapter to the other. So basically, each adapter overrides this method with their own definition.

您所感兴趣的:

ActiveRecord::Base.connection.quote_column_name "my_column"

这篇关于引用列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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