Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Hash conditions fail with PostgreSQL ENUMs

#6091

Cannot query a table by an enumerated field.

For example, when gender is a PostgreSQL enum:

irb(main):004:0> Person.where(:gender => "male").first
NoMethodError: undefined method `visit_Arel_Attributes_Undefined' for #<Arel::Visitors::PostgreSQL:0x7f0c52d6b310>
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/visitor.rb:15:in `send'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/visitor.rb:15:in `visit'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:235:in `visit_Arel_Nodes_Equality'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/visitor.rb:15:in `send'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/visitor.rb:15:in `visit'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:109:in `visit_Arel_Nodes_Grouping'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/visitor.rb:15:in `send'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/visitor.rb:15:in `visit'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:89:in `visit_Arel_Nodes_SelectCore'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:89:in `map'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:89:in `visit_Arel_Nodes_SelectCore'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:77:in `visit_Arel_Nodes_SelectStatement'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:77:in `map'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:77:in `visit_Arel_Nodes_SelectStatement'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/postgresql.rb:24:in `visit_Arel_Nodes_SelectStatement'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/visitor.rb:15:in `send'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/visitor.rb:15:in `visit'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/visitor.rb:5:in `accept'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:19:in `accept'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:110:in `with_connection'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:17:in `accept'
    from /usr/local/lib/ruby/gems/1.8/gems/arel-2.0.4/lib/arel/tree_manager.rb:19:in `to_sql'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/relation.rb:64:in `to_a'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/relation/finder_methods.rb:333:in `find_first'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/relation/finder_methods.rb:122:in `first'
    from (irb):4

Querying via array substitution works:

Person.where(["gender=?", "male"]).first

This used to work in Rails 3.0.1.

Reported by JackC · November 30th, 2010 @ 05:22 PM

State: new
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh
    • Importance changed from to Low

    In edge rails both of these work.

    ree-1.8.7-2010.02 > Car.where(:name => 'honda').first
      Car Load (0.2ms)  SELECT "cars".* FROM "cars" WHERE "cars"."name" = 'honda' LIMIT 1
     => #<Car id: 1, name: "honda", color: nil> 
    ree-1.8.7-2010.02 > Car.where(["name = ?",'honda']).first
      Car Load (0.2ms)  SELECT "cars".* FROM "cars" WHERE (name = 'honda') LIMIT 1
     => #<Car id: 1, name: "honda", color: nil> 
    ree-1.8.7-2010.02 >
    

    December 1st, 2010 @ 05:14 AM

  2. Tom Hughes
    Tom Hughes

    Well rails 3.0.4 is out now, but this still seems to be failing?

    February 9th, 2011 @ 10:57 PM

  3. Tom Hughes
    Tom Hughes

    It seems the answer is to update arel - rails 3.0.4 was still only requiring arel 2.0.2 or later and I had 2.0.3 which was sufficient for that. Updating arel to 2.0.8 seems to fix the problem.

    February 10th, 2011 @ 12:39 AM