# Extra field as attribute in join table with has\_many :through

**URL:** <https://www.ruby-forum.com/t/extra-field-as-attribute-in-join-table-with-has-many-through/142581>\
**Category:** Rails\
**Created:** [July 9, 2008, 12:06pm UTC](https://www.ruby-forum.com/t/extra-field-as-attribute-in-join-table-with-has-many-through/142581 "2008-07-09T12:06:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [July 9, 2008, 12:06pm UTC](https://www.ruby-forum.com/t/extra-field-as-attribute-in-join-table-with-has-many-through/142581/1 "2008-07-09T12:06:48Z")

</div>

hi everyone,  
I’ve pasted some code over there [http://pastie.org/230343](http://pastie.org/230343) .

what I find strange is that the extra field in the join table called  
“quantity” doesn’t shows up in the Drug resultset, even if I added an  
attr\_accessor for good measure.

anybody knows why? I’m on edge rails.

thanks.

---

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [July 9, 2008, 12:12pm UTC](https://www.ruby-forum.com/t/extra-field-as-attribute-in-join-table-with-has-many-through/142581/2 "2008-07-09T12:12:06Z")

</div>

On Jul 9, 2:37 am, Claudio P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hi everyone,  
> I’ve pasted some code over therehttp://pastie.org/230343.
> 
> what I find strange is that the extra field in the join table called  
> “quantity” doesn’t shows up in the Drug resultset, even if I added an  
> attr\_accessor for good measure.

The attr\_accessor would actually squash the attribute if it were  
there.  
It’s normal not to get the join table attributes - that’s just the way  
rails does things. Your workaround with the explicit select clause  
should work, I expect that you are being fooled by ActiveRecord’s  
display of your instance of Drug, which only displays attributes from  
the drugs.table. Document.find(2).drugs.first.quantity should work  
(but get rid of the attr\_accessor first).

Fred

---

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [July 9, 2008, 5:28pm UTC](https://www.ruby-forum.com/t/extra-field-as-attribute-in-join-table-with-has-many-through/142581/3 "2008-07-09T17:28:25Z")

</div>

Frederick C. wrote:

> On Jul 9, 2:37ï¿½am, Claudio P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > hi everyone,  
> > I’ve pasted some code over therehttp://pastie.org/230343.
> > 
> > what I find strange is that the extra field in the join table called  
> > “quantity” doesn’t shows up in the Drug resultset, even if I added an  
> > attr\_accessor for good measure.
> 
> The attr\_accessor would actually squash the attribute if it were  
> there.  
> It’s normal not to get the join table attributes - that’s just the way  
> rails does things. Your workaround with the explicit select clause  
> should work, I expect that you are being fooled by ActiveRecord’s  
> display of your instance of Drug, which only displays attributes from  
> the drugs.table. Document.find(2).drugs.first.quantity should work  
> (but get rid of the attr\_accessor first).
> 
> Fred

One way to guarantee the method exists,  
and allow you to cast it to an integer would be;

class Drug \< ActiveRecord::Base  
def quantity  
self[:quantity].to\_i  
end  
end

But I don’t like it,  
because “quantity” isn’t actually a value of of the Drug,  
it’s a value of the join.

clearly;  
drug = document.drugs.first  
drug.quantity != drug.reload.quantity (which will always be 0)

I’d prefer the following syntax;

drug = documents.drugs.first  
quantity = documents.quantity\_of\_drug(drug)

class Document \< ActiveRecord::Base  
def quantity\_of\_drug(drug)  
if join = join\_for\_drug(drug)  
return join.quantity  
else  
return 0  
end  
end

private

def join\_for\_drug(drug)  
self.document\_drugs.detect {|dd| dd.drug == drug}  
end  
end

however you’d need to ensure you loaded drugs through document\_drugs,  
rather than by a direct join  
(not quite sure what rails does about this at the moment)

---

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [July 11, 2008, 6:52pm UTC](https://www.ruby-forum.com/t/extra-field-as-attribute-in-join-table-with-has-many-through/142581/4 "2008-07-11T18:52:49Z")

</div>

thanks for the suggestions, I’m going to do a bit of work on that as  
advertised.
