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.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
> 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.Instead of: 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) try this: options_from_collection_for_select(DevicesOid.find(:all, :conditions => [''device_id = ?'', session[:device_id]], :order => ''oid_id''), :id, ''oid.object'') Nelson ------------------ Basedex - A Collaborative Index to organize and collect everything related to Ruby on Rails http://blazingrails.basedex.com/index/8 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Nelson, Thank you for the response! Unfortunately using your snippet throws an undefined method error for "oid.object", I''m not entirely sure as to why. Any further pointers would be greatly appreciated. Nelson Hsu wrote:>> 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. > > Instead of: > > 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) > > try this: > > options_from_collection_for_select(DevicesOid.find(:all, :conditions => > [''device_id = ?'', session[:device_id]], :order => ''oid_id''), :id, > ''oid.object'') > > Nelson > > > > ------------------ > Basedex - A Collaborative Index to organize and collect everything > related to Ruby on Rails > http://blazingrails.basedex.com/index/8-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jason Madigan wrote:> Nelson, > > Thank you for the response! Unfortunately using your snippet throws an > undefined method error for "oid.object", I''m not entirely sure as to > why. > > Any further pointers would be greatly appreciated. >Hmm... in conjunction with my snippet, try adding a wrapper method in the Devices0id: def oid_object oid.object end And in the snippet, change ''oid.object'' to ''oid_object'' This feels a bit hacky to me, but hopefully it can enable you to keep going with your project, and hopefully someone will point out the ''right'' way to do it. Nelson -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Nelson, Thank you again for the response. There must be something wrong with my models because adding that wrapper just throws a nil object error. I''ll post if I find a solution. Nelson Hsu wrote:> Jason Madigan wrote: >> Nelson, >> >> Thank you for the response! Unfortunately using your snippet throws an >> undefined method error for "oid.object", I''m not entirely sure as to >> why. >> >> Any further pointers would be greatly appreciated. >> > > Hmm... in conjunction with my snippet, try adding a wrapper method in > the Devices0id: > > def oid_object > oid.object > end > > And in the snippet, change ''oid.object'' to ''oid_object'' > > This feels a bit hacky to me, but hopefully it can enable you to keep > going with your project, and hopefully someone will point out the > ''right'' way to do it. > > Nelson-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---