xiaoggse 2009-04-18 01:59
浏览 368
已采纳

关于find方法中boolean型的问题

关于find方法中boolean型的问题
悬赏:5 发布时间:2009-04-18 提问人:阿拉觉得方腊; (初级程序员)

Message.find(:first, :conditions => {:read => true})
=> #

为什么上面的方法可以,下面的方法就不行
其中t.column :read, :boolean
谢谢指点!

Message.find(:first, :conditions => ['read = ?', true])
ActiveRecord::StatementInvalid: Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read = 1) LIMIT 1' at line 1: SELECT * FROM messages WHERE (read = 1) LIMIT 1
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:188:in log'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/mysql_adapter.rb:309:in
execute'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/mysql_adapter.rb:563:in select'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in
select_all_without_query_cache'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in select_all'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:635:in
find_by_sql'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:1490:in find_every'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:1452:in
find_initial'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:587:in `find'
from (irb):3
[b]问题补充:[/b]
我的问题是文什么上面的行,下面的不行。
而且我成功执行过,

Album.find(:first, :conditions => ['user_id = ? and id = ? and active = ?',1,3, true])
=> #

我感觉就是很矛盾很困惑,怎么有的可以,有的不可以,

  • 写回答

6条回答 默认 最新

  • horace_lee 2009-04-20 13:11
    关注

    问题在于:

       read这个字段名,也就是说你把字段名改成read1,再写就两种查询都可以用了。
    

    那么,为什么read做字段名
    [code="ruby"]Message.find(:first, :conditions => {:read => true}) [/code]

    仍然,可以用呢?

    因为,对于Rails而言,两种查询转换成的sql语句不太一样,

    [code="ruby"]Message.find(:first, :conditions => ['read = ?', true]) [/code]
    会转换成:
    [code="sql"]SELECT * FROM messages WHERE (read = 1) LIMIT 1[/code]
    这时就会有保留字做属性名的语法错误,当然,你把read改成read1是不会出错的。

    但是,
    [code="ruby"]Message.find(:first, :conditions => {:read => true})[/code]
    会转换成
    [code="sql"]SELECT * FROM messages WHERE (messages.read = 1) LIMIT 1[/code]
    当然,就不会有属性名是保留字的错误。

    至于
    [code="ruby"]Album.find(:first, :conditions => ['user_id = ? and id = ? and active = ?',1,3, true]) [/code]
    能执行,自然是因为,没有保留字的原因了。

    :arrow:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog