# Getting data from a through table

**URL:** <https://www.ruby-forum.com/t/getting-data-from-a-through-table/185389>\
**Category:** Rails\
**Created:** [March 19, 2010, 4:50am UTC](https://www.ruby-forum.com/t/getting-data-from-a-through-table/185389 "2010-03-19T04:50:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [March 19, 2010, 4:50am UTC](https://www.ruby-forum.com/t/getting-data-from-a-through-table/185389/1 "2010-03-19T04:50:44Z")

</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

`
class AttributeObjects < ActiveRecord::Base
  belongs_to :attributes
  belongs_to :objects
end
class Attributes < ActiveRecord::Base
has_many :attribute_objects
has_many :objects, :through => :attribute_objects
end
class Objects < ActiveRecord::Base
has_many :attribute_objects
has_many :attributes, :through => :attribute_objects
end
`

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:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [March 19, 2010, 9:29am UTC](https://www.ruby-forum.com/t/getting-data-from-a-through-table/185389/2 "2010-03-19T09:29:55Z")

</div>

On 19 March 2010 03:49, dan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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|  
attribute.name …  
end

you do this:  
@object.attribute\_objects.each do |attribute\_object|  
attribute\_object.attribute.name …  
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)**
>
> 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:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [March 19, 2010, 9:24pm UTC](https://www.ruby-forum.com/t/getting-data-from-a-through-table/185389/3 "2010-03-19T21:24:08Z")

</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  
def top\_level  
all(:joins =\> :attribute, :conditions =\> “attributes.parent\_id  
is null”)  
end

```
def children(id)
  all(:joins => :attribute, :conditions => ["attributes.parent_id

```

= ?", id])  
end  
end

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