Rails 3 Association NoMethodError

时间:2011-10-07 01:31:05

标签: ruby-on-rails ruby-on-rails-3 associations

我对Rails相对较新,并且在Deep端有一点点。 我正在使用预先存在的MSSQL DB在Rails 3中工作,并且必须改进模型才能适应。

我似乎创建了我的模型而没有太多问题,但是却遇到了关联问题。

这是我的两个表格的架构

  create_table "ip_addresses", :id => false, :force => true do |t|
    t.integer  "id",                        :null => false
    t.integer  "computer_id",               :null => false
    t.string   "ip",  :limit => 64, :null => false
    t.string   "ip_subnet",   :limit => 64, :null => false
    t.datetime "timestamp",                 :null => false
    t.datetime "created_at"
    t.datetime "updated_at"
  end



 create_table "computers", :id => false, :force => true do |t|
    t.integer  "id",                                                                    :null => false
    t.string   "uid",                      :limit => 128
    t.string   "enclosure_uid",            :limit => 128
    t.string   "name",                     :limit => 128
    t.integer  "status_id",                                                             :null => false
    t.integer  "designation_id",                                                        :null => false
    t.integer  "hardware_type_id",                                                      :null => false
    t.string   "hardware_model",           :limit => 128
    t.string   "processor_model",          :limit => 128
    t.integer  "processor_speed"
    t.integer  "processor_cores_per_proc", :limit => 1
    t.integer  "processor_count"
    t.decimal  "memory",                                  :precision => 9, :scale => 2
    t.decimal  "physical_disk",                           :precision => 9, :scale => 2
    t.integer  "san_disk"
    t.string   "os_name",                  :limit => 128
    t.string   "os_version",               :limit => 128
    t.string   "os_patchlevel",            :limit => 128
    t.integer  "campus_id",                                                             :null => false
    t.text     "description"
    t.datetime "modification_date",                                                     :null => false
    t.datetime "os_installdate"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

以下是两个模型

class Computer < ActiveRecord::Base
  has_many :ip_addresses
end


class IpAddress < ActiveRecord::Base
  belongs_to :computer
end

当我使用IpAddress类时,似乎协会正在工作,但不是来自Computer类。

见下文。

>> IpAddress.first.computer.name
"GC-PRD-PS02"
>> Computer.first.ip_addresses.ip
NoMethodError: undefined method `ip' for #<Class:0x10291cb18>
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.10/lib/active_record/base.rb:1014:in `method_missing'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.10/lib/active_record/associations/association_collection.rb:444:in `send'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.10/lib/active_record/associations/association_collection.rb:444:in `method_missing'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.10/lib/active_record/base.rb:1127:in `with_scope'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.10/lib/active_record/associations/association_proxy.rb:207:in `send'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.10/lib/active_record/associations/association_proxy.rb:207:in `with_scope'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.10/lib/active_record/associations/association_collection.rb:440:in `method_missing'
    from (irb):40

奇怪的是我可以使用

查看IpAddress类的整个行对象
>> Computer.first.ip_addresses
[#<IpAddress id: 175, computer_id: 687, ip: "10.0.246.80", ip_subnet: "255.255.255.192", timestamp: "2011-08-03 11:17:57", created_at: "2011-10-07 01:06:16", updated_at: "2011-10-07 01:06:16">, #<IpAddress id: 176, computer_id: 687, ip: "192.168.234.235", ip_subnet: "255.255.255.255", timestamp: "2011-08-03 11:17:57", created_at: "2011-10-07 01:06:16", updated_at: "2011-10-07 01:06:16">, #<IpAddress id: 177, computer_id: 687, ip: "192.168.159.1", ip_subnet: "255.255.255.0", timestamp: "2011-08-03 11:17:57", created_at: "2011-10-07 01:06:16", updated_at: "2011-10-07 01:06:16">, #<IpAddress id: 178, computer_id: 687, ip: "192.168.42.1", ip_subnet: "255.255.255.0", timestamp: "2011-08-03 11:17:57", created_at: "2011-10-07 01:06:16", updated_at: "2011-10-07 01:06:16">]

我已经搜索了很多,试图解决这个问题。我确信这只是我做协会的方式。任何帮助都会得到很多赞赏。

谢谢, 罗布

2 个答案:

答案 0 :(得分:4)

由于Computer.first.ip_addresses是一个数组,您需要执行以下操作:

Computer.first.ip_addresses.first.ip

如果你想要拳头ip

Computer.first.ip_addresses.map(&:ip)

如果你想要所有的ips

答案 1 :(得分:1)

在集合上调用ip - 看起来像是问题。 Computer.first.ip_addresses[0].ip应该有用(假设ip_addresses至少包含一个元素)。

另外,对于学习,请注意puts Computer.first.ip_addresses.class给你的内容。