Hi All,
I think Im having a mental block here. For some reason I just cant get
my head round relationships.
What I have is the following.
4 tables
- Hosts
- Software
- OperatingSystem
- Company
In my terms (which are most likely wrong) a company can have lots of
hosts, a host has lots of software, a host has one operating system.
So should my models have the following relations in them?
class Host < ActiveRecord::Base
belongs_to :customer
has_and_belongs_to_many :software
belongs_to :operatingsystems
end
class Customer < ActiveRecord::Base
has_many :hosts
end
class Operatingsystem < ActiveRecord::Base
has_many :hosts
end
class Software < ActiveRecord::Base
set_table_name “software” #not softwares!
has_and_belongs_to_many :hosts
end
The above seems to work, but I cant do things like
@hosts = Hosts.find(:all)
for host in @hosts
puts (host.operatingsystem.description)
end
as there doesnt seem to be a relationship between the host and the OS
(or software or customer) I have to do the following to get the OS (or
software or customer)
@host = Hosts.find(:all)
for host in @hosts
@os = Operatingsystem.find(host.id)
puts (@os.description)
end
Which is OK, but I think is not right somewhere.
Can someone point me to some good docs on this or tell me where I am
going wrong (ive read 4 days on rails, AWDWR etc)
Many thanks for the help
Jonathan