# :includes with condition - accessing ids

**URL:** <https://www.ruby-forum.com/t/includes-with-condition-accessing-ids/88691>\
**Category:** Rails\
**Created:** [March 13, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/includes-with-condition-accessing-ids/88691 "2007-03-13T16:01:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [March 13, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/includes-with-condition-accessing-ids/88691/1 "2007-03-13T16:01:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [March 13, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/includes-with-condition-accessing-ids/88691/2 "2007-03-13T20:09:50Z")

</div>

> 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](http://blazingrails.basedex.com/index/8)

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [March 13, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/includes-with-condition-accessing-ids/88691/3 "2007-03-13T21:39:30Z")

</div>

Jason M. 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

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [March 13, 2007, 10:04pm UTC](https://www.ruby-forum.com/t/includes-with-condition-accessing-ids/88691/4 "2007-03-13T22:04:28Z")

</div>

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 H. wrote:

> Jason M. 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

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [March 13, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/includes-with-condition-accessing-ids/88691/5 "2007-03-13T21:27:57Z")

</div>

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 H. 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](http://blazingrails.basedex.com/index/8)
