Topic: What's the best way to implement this design?(association related)

Hey again rails programmers. As I continue to develop and learn RoR, I came across a few problems implementing associations(started using them a few days ago) between several models. Illustrated below is a table diagram of the several entities that I have to work with.

http://i74.photobucket.com/albums/i259/ReptileMonkey/network_schema.gif

1)The user should be able to create a "device" on one 'form' which includes all of the models.

2)A "network" has many "plans." A "plan" can belong to several "networks."

3)A "plan" has many "devices." A "device can belong to several "plans."

4)A "network" has many "devices" through "plans." A "device" has many "networks" through "plans." (is this one even possible?)

5)A "device" has many "macs." A "mac" can only belong to one "device."

6)A "device" has many "ips." A "ip" can only belong to one "device."

7)A "device" has many "functions." A "function" can have many "devices."

8)A "device" has many "softwares." A "software" can have many "devices."

So far, I have come up with the following.

class Network < ActiveRecord::Base
  has_and_belongs_to_many :plans
end

class Plan < ActiveRecord::Base
  has_and_belongs_to_many :networks
  has_and_belongs_to_many :devices
end

class Device < ActiveRecord::Base
  has_and_belongs_to_many :plans
  has_many :macs
  has_many :ips
  has_and_belongs_to_many :functions
  has_and_belongs_to_many :softwares
end

class Mac < ActiveRecord::Base
  has_one :device
end

class Ip < ActiveRecord::Base
  has_one :device
end

class Function < ActiveRecord::Base
  has_and_belongs_to_many :devices
end

class Software < ActiveRecord::Base
  has_and_belongs_to_many :devices
end


I don't know if I have done the right thing here, and I'm not sure if it is possible to implement step "4" of the process. Any insight and help is greatly appreciated. big_smile

http://www.webyfi.com
It's not the size of the app that matters, it's how you code it. ~ railsenvy

Re: What's the best way to implement this design?(association related)

You're definitely on the right track.  You can totally do #4 too:

class Network < ActiveRecord::Base
  has_and_belongs_to_many :plans
  has_many :devices, :through => :plans
end

the has_and_belongs_to_many calls will work, but most of the Rails community is starting to walk away from this style.  Instead of saying:
class Function < ActiveRecord::Base
  has_and_belongs_to_many :devices
end
class Device < ActiveRecord::Base
  has_and_belongs_to_many :functions
end

Now a lot of folks are making an explicit join model.  This way you can track things like when the join was made (when a device got access to a function) who did it, what other parties were involved, etc.

class Function < ActiveRecord::Base
  has_many :devices_functions
end
class Device < ActiveRecord::Base
  has_many :device_functions
end
class DeviceFunctions < ActiveRecord::Base
  belongs_to :device
  belongs_to :function
end

Note: if you use the second option you can name the join table whatever you want.  In this case I made it more like standard english and singularized device.

Re: What's the best way to implement this design?(association related)

Thanks for your time and explanations danger! Much appreciated! smile

http://www.webyfi.com
It's not the size of the app that matters, it's how you code it. ~ railsenvy