I’m quite new to Rails and I’ve come up against a little problem which
hopefully you guys can shed some light on. I have a multiple select form
element, populated with OIDs associated with a given network devices
(I’m building a small RoR SNMP app). Let me give you a quick run down of
how the models/DB is set up:
“oids” table - fields: id, oid, object, type, description
“devices” table - fields: id, ip, network_id
"devices_oids_ join table - fields: id, device_id, oid_id
The models look something like:
class Device < ActiveRecord::Base
has_many :devices_oid, :dependent => true
has_many :oids, :through => :devices_oid
belongs_to :network
end
class Oid < ActiveRecord::Base
has_many :devices_oid, :dependent => true
has_many :devices, :through => :devices_oid
end
class DevicesOid < ActiveRecord::Base
belongs_to :device
belongs_to :oid
end
To get this up and running, I’ve been using the following in a view:
<% form_tag :action => “remove_oid_from_device” do %>
<%= select_tag(‘oids_to_rem[]’,
options_from_collection_for_select(Oid.find(:all, :include =>
:devices_oid, :conditions => “oids.id = devices_oids.oid_id AND
devices_oids.device_id = #{session[:device_id]}”, :order => “oids.id”),
:id, :object), :multiple => true, :class => “oid_select”) %>
<%= submit_tag ‘Remove OID’ %>
I’ll obviously move this to a model class method later, but for the
moment while testing I’m simply using it in a view.
The problem I’m having is that the above code fragment constructs a
selection form with the value of each selection associated with a given
ID in the “OID” model. What I want to have is for each of these values
to be associated with a given ID from the “DevicesOid”. My first thought
was to change “:id, :object” to “‘devices_oids.id’, object”, but this
doesn’t work. I’m open to any ideas or suggestions you might have since
looking at it right now it appears really ugly.