新手问题 关于 ActiveRecord 使用 set_table_name 的诡异问题

santochancf · 2012年09月20日 · 最后由 santochancf 回复于 2012年09月21日 · 3502 次阅读

简单使用 ActiveRecord 来做数据查询

class Tmnl < ActiveRecord::Base   
  set_table_name  "tmnl_status"  
  #set_primary_key "STATUS_ID"
end

p Tmnl.first

但是这个表,不知特殊在哪里,其他表都没问题,总会在查询是报错:

ArgumentError: wrong number of arguments (1 for 0)

贴 log 别吝啬

是的 #1 楼 @Rei


ArgumentError: wrong number of arguments (1 for 0)
       __run_callback at /Users/santochan/.rvm/gems/jruby-1.6.7.2/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:399
  _run_find_callbacks at /Users/santochan/.rvm/gems/jruby-1.6.7.2/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:390
             __send__ at org/jruby/RubyBasicObject.java:1698
                 send at org/jruby/RubyKernel.java:2097
        run_callbacks at /Users/santochan/.rvm/gems/jruby-1.6.7.2/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81
            init_with at /Users/santochan/.rvm/gems/jruby-1.6.7.2/gems/activerecord-3.2.8/lib/active_record/base.rb:523
          instantiate at /Users/santochan/.rvm/gems/jruby-1.6.7.2/gems/activerecord-3.2.8/lib/active_record/inheritance.rb:68
          find_by_sql at /Users/santochan/.rvm/gems/jruby-1.6.7.2/gems/activerecord-3.2.8/lib/active_record/querying.rb:38
             collect! at org/jruby/RubyArray.java:2363
          find_by_sql at /Users/santochan/.rvm/gems/jruby-1.6.7.2/gems/activerecord-3.2.8/lib/active_record/querying.rb:38
   logging_query_plan at /Users/santochan/.rvm/gems/jruby-1.6.7.2/gems/activerecord-3.2.8/lib/active_record/explain.rb:40
          find_by_sql at /Users/santochan/.rvm/gems/jruby-1.6.7.2/gems/activerecord-3.2.8/lib/active_record/querying.rb:37
         exec_queries at /Users/santochan/.rvm/gems/jruby-1.6.7.2/gems/activerecord-3.2.8/lib/active_record/relation.rb:171
                 to_a at /Users/santochan/.rvm/gems/jruby-1.6.7.2/gems/activerecord-3.2.8/lib/active_record/relation.rb:160
   logging_query_plan at /Users/santochan/.rvm/gems/jruby-1.6.7.2/gems/activerecord-3.2.8/lib/active_record/explain.rb:40
                 to_a at /Users/santochan/.rvm/gems/jruby-1.6.7.2/gems/activerecord-3.2.8/lib/active_record/relation.rb:159
           find_first at /Users/santochan/.rvm/gems/jruby-1.6.7.2/gems/activerecord-3.2.8/lib/active_record/relation/finder_methods.rb:378
                first at /Users/santochan/.rvm/gems/jruby-1.6.7.2/gems/activerecord-3.2.8/lib/active_record/relation/finder_methods.rb:122
             __send__ at org/jruby/RubyBasicObject.java:1698
                 send at org/jruby/RubyKernel.java:2097
                 find at /Users/santochan/.rvm/gems/jruby-1.6.7.2/gems/activerecord-3.2.8/lib/active_record/relation/finder_methods.rb:105
             __send__ at org/jruby/RubyBasicObject.java:1704
                 find at /Users/santochan/.rvm/gems/jruby-1.6.7.2/gems/activerecord-3.2.8/lib/active_record/querying.rb:5
               (root) at table_to_csv.rb:44

表里面有什么字段?

#3 楼 @Rei 类型主要是 varchar2、date、number。 字段名没有什么特殊的,含有 status_id、status_ip、tmnl_no 这样格式的。 有的字段没有数据

#3 楼 @Rei 加入

puts Tmnl.column_names

可以正常执行,得到:

status_id
cp_no
terminal_ip
terminal_mask
terminal_port
dial_no
machine_id_cur
machine_ip_cur
conn_time_cur
machine_id_prev
machine_ip_prev
conn_time_prev
send
receive
status
last_beat_time
refresh_time
terminal_id
area_code
status_code
protocol_code
run_date
type_code
manufacturer
coll_mode

就是不能查询……

rails3.2 开始就使用下列方法来设置 table name self.table_name = "tmnl_status"

·set_table_name· 是 rails3.1 之前的方法

#7 楼 @foxzool 是的,开始我也是这样设置的,但目前好像就是查询数据会出错

应该就是 send 这个字段,覆盖了 .send 方法了,要改名。

抛错的那行代码是

str = object.send("_#{kind}_callbacks").compile(key, object)

#9 楼 @Rei 啊,有道理。我试试

#9 楼 @Rei 没有能成功重命名,问题依旧,只能修改数据库表么?

我搜了下,只搜到 activerecord-jdbc 有相关的问题,可能是 jruby 特有的?然后我新建了一个 app,用 send 字段没问题。

你试试不用 jruby,用 ruby 1.9.3。

#12 楼 @Rei 多谢提醒,让我开始排查 JDBC adapter 的问题,我使用的是 oracle_enhanced。然后发现确实是 send 字段的问题,但这个 adapter 可以将此列改名或者忽略,就是用于兼容旧的数据库表设计的。今早实验了一下,搞定了!😄

需要 登录 后方可回复, 如果你还没有账号请 注册新账号