# getting data from a through table

**URL:** <https://discuss.rubyonrails.org/t/getting-data-from-a-through-table/50238>\
**Category:** rubyonrails-talk\
**Created:** [March 19, 2010, 3:49am UTC](https://discuss.rubyonrails.org/t/getting-data-from-a-through-table/50238 "2010-03-19T03:49:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan11](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@dan11](https://discuss.rubyonrails.org/u/dan11)\
**Post date:** [March 19, 2010, 3:49am UTC](https://discuss.rubyonrails.org/t/getting-data-from-a-through-table/50238/1 "2010-03-19T03:49:10Z")

</div>

im using has\_many through and the through table has some data i want but i cant seem to access it using straight rails

\<code\> class AttributeObjects \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :attributes &nbsp;&nbsp;belongs\_to :objects end

class Attributes \< ActiveRecord::Base &nbsp;&nbsp;has\_many :attribute\_objects &nbsp;&nbsp;has\_many :objects, :through =\> :attribute\_objects end

class Objects \< ActiveRecord::Base &nbsp;&nbsp;has\_many :attribute\_objects &nbsp;&nbsp;has\_many :attributes, :through =\> :attribute\_objects end

\</code\>

now imagine that attribute\_objects also contains a rating so when i say, object.attributes, id like to get the rating too that doesnt seem to be possible with the has\_many relationship if i could force the join on the object attributes to bring the attribute\_objects fields also...maybe

i think i have to write custom sql for this

---

<div class="post-metadata">

**Author:** ![Michael\_Pavling](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Pavling](https://discuss.rubyonrails.org/u/Michael_Pavling)\
**Post date:** [March 19, 2010, 8:28am UTC](https://discuss.rubyonrails.org/t/getting-data-from-a-through-table/50238/2 "2010-03-19T08:28:09Z")

</div>

> now imagine that attribute\_objects also contains a rating so when i say, object.attributes, id like to get the rating too that doesnt seem to be possible with the has\_many relationship if i could force the join on the object attributes to bring the attribute\_objects fields also...maybe

If you want to access properties of AttributeObjects [1], you can iterate the collection of them, not the collection of attributes. So instead of:

@object.attributes.each do |attribute| &nbsp;&nbsp;attribute.name .... end

you do this: @object.attribute\_objects.each do |attribute\_object| &nbsp;&nbsp;attribute\_object.attribute.name .... &nbsp;&nbsp;attribute\_object.rating .... end

> i think i have to write custom sql for this

You can if you want... but you don't \*have\* to. You can possibly use the ":finder\_sql" option of the association to specify your own conditions for populating the collection (I've been fiddling with it here for 10mins, and not got it working... post up some working code if it does it for you 🙂

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Active Record Associations Associations are a set of macro-like class methods for tying objects together through foreign keys.

[1] All your class names are plural; they should really be singular...

---

<div class="post-metadata">

**Author:** ![dan11](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@dan11](https://discuss.rubyonrails.org/u/dan11)\
**Post date:** [March 19, 2010, 8:22pm UTC](https://discuss.rubyonrails.org/t/getting-data-from-a-through-table/50238/3 "2010-03-19T20:22:39Z")

</div>

thanks for the suggestions

attributes is a self inherited table so i need to get them i order of top level and down this is part of my current solution

has\_many :objects\_attributes do &nbsp;&nbsp;&nbsp;&nbsp;def top\_level &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;all(:joins =\> :attribute, :conditions =\> "attributes.parent\_id is null") &nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def children(id) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;all(:joins =\> :attribute, :conditions =\> ["attributes.parent\_id = ?", id]) &nbsp;&nbsp;&nbsp;&nbsp;end end

this allows me to access all fields in order desired thanks for your help
