答案编辑:
问题似乎是我有一个名为" class"的列。 - 我发现gem似乎允许它工作,尽管使用保留字作为列名。谢谢大家的帮助。
问题:
我有一个现有的MySQL数据库我正在尝试构建一个应用程序并且原始表名不符合约定,所以我在模型中设置它。
型号:
class Event < ActiveRecord::Base
self.table_name = "event_20140204"
end
我已尝试使用控制器/视图进行多项测试,但我认为最简单的答案可能来自控制台。我是Rails的新手,所以我希望我能正确地展示问题:
column_names示例:
irb(main):004:0> Event.column_names
=> ["sid", "signature", "timestamp", "unified_event_id", "unified_event_ref", "unified_ref_time", "priority", continued ... ]
示例记录(第一个):
irb(main):001:0> Event.first
Event Load (0.2ms) SELECT `event_20140204`.* FROM `event_20140204` LIMIT 1
=> #<Event sid: 3, signature: "Example signature", timestamp: "2014-02-04 00:02:08", unified_event_id: 3, unified_event_ref: 3, unified_ref_time: "2014-02-04 00:02:08", priority: 1, [ continued ... ] >
然后尝试显示一个简单的字段,它崩溃了这个错误:
irb(main):002:0> e = Event.first
< same as above, code omitted >
irb(main):003:0> e.timestamp
NoMethodError: undefined method `find_generated_attribute_method' for "misc-attack":String
from /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/attribute_methods.rb:199:in `method_missing'
from (irb):3
from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in `start'
from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in `start'
from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in `console'
from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.1.1/lib/rails/commands.rb:17:in `<top (required)>'
from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `require'
from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `block in require'
from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in `load_dependency'
from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `require'
from /var/www/html/ir/bin/rails:8:in `<top (required)>'
from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `load'
from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `block in load'
from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in `load_dependency'
from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `load'
from /usr/local/lib/ruby/gems/2.0.0/gems/spring-1.1.3/lib/spring/commands/rails.rb:6:in `call'
from /usr/local/lib/ruby/gems/2.0.0/gems/spring-1.1.3/lib/spring/command_wrapper.rb:38:in `call'
from /usr/local/lib/ruby/gems/2.0.0/gems/spring-1.1.3/lib/spring/application.rb:180:in `block in serve'
from /usr/local/lib/ruby/gems/2.0.0/gems/spring-1.1.3/lib/spring/application.rb:153:in `fork'
from /usr/local/lib/ruby/gems/2.0.0/gems/spring-1.1.3/lib/spring/application.rb:153:in `serve'
from /usr/local/lib/ruby/gems/2.0.0/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run'
from /usr/local/lib/ruby/gems/2.0.0/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop'
from /usr/local/lib/ruby/gems/2.0.0/gems/spring-1.1.3/lib/spring/application.rb:122:in `run'
from /usr/local/lib/ruby/gems/2.0.0/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in `<top (required)>'
from /usr/local/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/local/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
misc-attack
是另一个领域。当我尝试使用e.colum_name显示任何列时会发生这种情况。
另一个简单的测试:
irb(main):041:0> e = Event.all.limit(5)
Event Load (1.6ms) SELECT `event_20140204`.* FROM `event_20140204` LIMIT 5
(Object doesn't support #inspect)
=>
irb(main):042:0> e.count
(0.3ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM `event_20140204` LIMIT 5) subquery_for_count
=> 5
irb(main):043:0> e.each do |event|
irb(main):044:1* event.timestamp
irb(main):045:1> end
Event Load (0.2ms) SELECT `event_20140204`.* FROM `event_20140204` LIMIT 5
NoMethodError: undefined method `[]' for nil:NilClass
from /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/attribute_methods/read.rb:111:in `read_attribute'
from /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/attribute_methods.rb:47:in `__temp__36c6163737'
from /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/core.rb:215:in `init_with'
from /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/persistence.rb:52:in `instantiate'
from /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/querying.rb:48:in `block in find_by_sql'
from /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/result.rb:55:in `block in each'
from /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/result.rb:55:in `each'
from /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/result.rb:55:in `each'
from /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/querying.rb:48:in `map'
from /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/querying.rb:48:in `find_by_sql'
from /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/relation.rb:603:in `exec_queries'
from /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/relation.rb:487:in `load'
from /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/relation.rb:231:in `to_a'
from /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/relation/delegation.rb:46:in `each'
from (irb):43
from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in `start'
from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in `start'
from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in `console'
from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.1.1/lib/rails/commands.rb:17:in `<top (required)>'
from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `require'
from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `block in require'
from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in `load_dependency'
from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `require'
from /var/www/html/ir/bin/rails:8:in `<top (required)>'
from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `load'
from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `block in load'
from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in `load_dependency'
from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `load'
from /usr/local/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/local/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'irb(main):046:0>
我的控制器/视图测试中NoMethodError: undefined method []' for nil:NilClass
是我痛苦的原因。我会做一个简单的do / each循环来显示每列的数据,它总是会显示带有undefined method
问题的Rails错误屏幕。
有没有人有任何想法?
编辑:
来自MySQL的(未修改的)架构,如请求的那样。这是一个sguil事件数据库:
+-------------------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+----------------------+------+-----+---------+-------+
| sid | int(10) unsigned | NO | PRI | NULL | |
| cid | int(10) unsigned | NO | PRI | NULL | |
| signature | varchar(255) | NO | MUL | NULL | |
| signature_gen | int(10) unsigned | NO | | NULL | |
| signature_id | int(10) unsigned | NO | | NULL | |
| signature_rev | int(10) unsigned | NO | | NULL | |
| timestamp | datetime | NO | MUL | NULL | |
| unified_event_id | int(10) unsigned | YES | | NULL | |
| unified_event_ref | int(10) unsigned | YES | | NULL | |
| unified_ref_time | datetime | YES | | NULL | |
| priority | int(10) unsigned | YES | | NULL | |
| class | varchar(20) | YES | | NULL | |
| status | smallint(5) unsigned | YES | MUL | 0 | |
| src_ip | int(10) unsigned | YES | MUL | NULL | |
| dst_ip | int(10) unsigned | YES | MUL | NULL | |
| src_port | int(10) unsigned | YES | MUL | NULL | |
| dst_port | int(10) unsigned | YES | MUL | NULL | |
| icmp_type | tinyint(3) unsigned | YES | MUL | NULL | |
| icmp_code | tinyint(3) unsigned | YES | MUL | NULL | |
| ip_proto | tinyint(3) unsigned | YES | | NULL | |
| ip_ver | tinyint(3) unsigned | YES | | NULL | |
| ip_hlen | tinyint(3) unsigned | YES | | NULL | |
| ip_tos | tinyint(3) unsigned | YES | | NULL | |
| ip_len | smallint(5) unsigned | YES | | NULL | |
| ip_id | smallint(5) unsigned | YES | | NULL | |
| ip_flags | tinyint(3) unsigned | YES | | NULL | |
| ip_off | smallint(5) unsigned | YES | | NULL | |
| ip_ttl | tinyint(3) unsigned | YES | | NULL | |
| ip_csum | smallint(5) unsigned | YES | | NULL | |
| last_modified | datetime | YES | MUL | NULL | |
| last_uid | int(10) unsigned | YES | | NULL | |
| abuse_queue | enum('Y','N') | YES | | NULL | |
| abuse_sent | enum('Y','N') | YES | | NULL | |
+-------------------+----------------------+------+-----+---------+-------+
答案 0 :(得分:1)
Rails为每个列生成访问器方法。这个的一个推论是一个名为&#34; class&#34;的属性。是一件非常糟糕的事情:访问器方法覆盖了该名称的内置方法,所以
event.class
返回该属性的值而不是其类,它将调用各种不可思议的错误
我不知道处理这个问题的好方法 - 理想情况下,您希望能够在不更改源表的情况下将属性别名化为安全名称。