Reputation: 35
I am relatively new to Rails and am a little bit in the Deep end. I am working in Rails 3 with a pre-existing MSSQL DB and having to retrofit out the models to fit.
I seem to have created my models without too many issues but have hit an issue with associations.
Here's my Schema for the two tables in question
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
Here are the two models
class Computer < ActiveRecord::Base
has_many :ip_addresses
end
class IpAddress < ActiveRecord::Base
belongs_to :computer
end
It seems like the Association is working when I use the IpAddress Class, but not from the Computer class.
see below.
>> 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
What is strange is that I can see the entire row object of the IpAddress Class by using
>> 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">]
I have search quite a bit to try and work this one out. I'm sure it's just my way of doing associations. Any help would be much Appreciated.
Thanks, Rob
Upvotes: 2
Views: 405
Reputation: 1806
As Computer.first.ip_addresses is an array, you need to do :
Computer.first.ip_addresses.first.ip
if you want the fist ip
Computer.first.ip_addresses.map(&:ip)
if you want all ips
Upvotes: 4
Reputation: 65467
Calling ip
on a collection - that looks like the problem. Computer.first.ip_addresses[0].ip
should work (presuming ip_addresses
contains at least one element).
Also for learning, note what puts Computer.first.ip_addresses.class
gives you.
Upvotes: 1