# Is there a "has\_one ..., :through"?

**URL:** <https://www.ruby-forum.com/t/is-there-a-has-one-through/119055>\
**Category:** Rails\
**Created:** [November 2, 2007, 6:03pm UTC](https://www.ruby-forum.com/t/is-there-a-has-one-through/119055 "2007-11-02T18:03:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [November 2, 2007, 6:03pm UTC](https://www.ruby-forum.com/t/is-there-a-has-one-through/119055/1 "2007-11-02T18:03:34Z")

</div>

I have found a few posts on various sites that this was something that  
was going to be available. The posts were all over a year ago, but it  
doesn’t seem like it is available in 1.2 unless I am missing something  
(which I hope is true)

I have a member and office table that each have an address. Any  
advice on the best way to do this. I have also seen a few posts  
saying to use delegates, but without much info on how to do so and I  
can’t find any info on delegates in the docs.

Any advice on the best way to make these relationships is appreciated.

Thanks

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [November 2, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/is-there-a-has-one-through/119055/2 "2007-11-02T19:11:30Z")

</div>

On Nov 2, 10:03 am, chris [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Any advice on the best way to make these relationships is appreciated.
> 
> Thanks

are these the posts youre talking about?  
[http://railsexpress.de/blog/articles/2006/09/16/piggy-back-plugin-updated](http://railsexpress.de/blog/articles/2006/09/16/piggy-back-plugin-updated)  
[http://www.artofmission.com/articles/2007/6/21/has\_one-through](http://www.artofmission.com/articles/2007/6/21/has_one-through)  
[http://entren.ivanyvenian.com/inclusiones-anidadas-asociaciones/](http://entren.ivanyvenian.com/inclusiones-anidadas-asociaciones/)

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [November 2, 2007, 7:29pm UTC](https://www.ruby-forum.com/t/is-there-a-has-one-through/119055/3 "2007-11-02T19:29:04Z")

</div>

first 2 yes, last one no.

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [November 2, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/is-there-a-has-one-through/119055/4 "2007-11-02T19:46:41Z")

</div>

if I take the long approach of creating a get and set method, is there  
a short way to make the association between the address and member  
table without having to create a model out of the associative table?

class member  
…  
#properties  
def address  
if @address.nil?  
@address = Address.find\_by\_member\_id(self.id) unless  
@address.nil?  
@address  
end

def address=(value)  
@address = value  
end

#methods  
def after\_insert  
@address.save  
#connect the tables here  
end  
…  
end

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [November 3, 2007, 4:09pm UTC](https://www.ruby-forum.com/t/is-there-a-has-one-through/119055/5 "2007-11-03T16:09:50Z")

</div>

the guy who wrote the second article reference, Ryan H., is  
working on a patch for Rails to add the has\_one :through.

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [November 2, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/is-there-a-has-one-through/119055/6 "2007-11-02T20:24:07Z")

</div>

or there is this way, but now I have a address collection within the  
model. It is rather difficult to believe that Rails would not have a  
better way of making associations of this sort.

class Member  
has\_and\_belongs\_to\_many :addresses, :join\_table  
=\> :members\_addresses

#properties  
def address  
return self.addresses[0] unless self.addresses.nil?  
nil  
end

def address=(value)  
self.addresses = [] if self.addresses.nil?  
self.addresses \<\< value  
end  
end

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [November 3, 2007, 4:20pm UTC](https://www.ruby-forum.com/t/is-there-a-has-one-through/119055/7 "2007-11-03T16:20:21Z")

</div>

In my case I have an address table that is linked to a few tables  
within the database. It turns out that in my case polymorphic  
association is probably the best bet anyway.

Thanks for letting me know though, I am sure the plugin will come in  
handy someday.
