# Has\_many :through=\>one

**URL:** <https://www.ruby-forum.com/t/has-many-through-one/201650>\
**Category:** Rails\
**Created:** [January 12, 2011, 10:37am UTC](https://www.ruby-forum.com/t/has-many-through-one/201650 "2011-01-12T10:37:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [January 12, 2011, 10:37am UTC](https://www.ruby-forum.com/t/has-many-through-one/201650/1 "2011-01-12T10:37:18Z")

</div>

Hi everyone.

I am facing a problem with has\_many :through with the following model  
relations:

class User \< AR  
has\_one :profile  
has\_many :files, :through =\> :profile  
end

class Profile \< AR  
belongs\_to :user  
has\_many :files  
end

class File \< AR  
belongs\_to :profile  
end

While the call user.files succeeds with no problem, the following  
assignment fails.

f = File.new  
u = User.first  
u.files \<\< f # FAILURE HERE!

Rails spits out a  
ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection  
exception.

Can anyone shed some light on this ?

Cheers,

sergioL

---

<div class="post-metadata">

**Author:** ![malvadomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@malvadomx](https://www.ruby-forum.com/u/malvadomx)\
**Post date:** [January 12, 2011, 10:49am UTC](https://www.ruby-forum.com/t/has-many-through-one/201650/2 "2011-01-12T10:49:19Z")

</div>

On 12 January 2011 09:37, Sergio L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Profile \< AR  
> belongs\_to :user  
> has\_many :files  
> end
> 
> class File \< AR  
> belongs\_to :profile  
> end

I think you might want to reconsider using the name File as this is  
already a class in Ruby. Whether this is the cause of your problem I  
don’t know, but I would try that first.

Colin

---

<div class="post-metadata">

**Author:** ![malvadomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@malvadomx](https://www.ruby-forum.com/u/malvadomx)\
**Post date:** [January 12, 2011, 10:51am UTC](https://www.ruby-forum.com/t/has-many-through-one/201650/3 "2011-01-12T10:51:51Z")

</div>

On 12 January 2011 09:37, Sergio L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can anyone shed some light on this ?

Exactly what the error says - you can’t have a has\_many through a  
has\_one

Either chain the method calls to your user’s profiles files:

user = User.first  
files = user.profile.files

…or create a delegate method in user to return the profile’s files:

class User \< AR  
has\_one :profile  
def files  
self.profile ? self.profile.files :   
end  
end

HTH

---

<div class="post-metadata">

**Author:** ![malvadomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@malvadomx](https://www.ruby-forum.com/u/malvadomx)\
**Post date:** [January 12, 2011, 10:53am UTC](https://www.ruby-forum.com/t/has-many-through-one/201650/4 "2011-01-12T10:53:06Z")

</div>

On 12 January 2011 09:47, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think you might want to reconsider using the name File as this is  
> already a class in Ruby. Whether this is the cause of your problem I  
> don’t know, but I would try that first.

oh yeah… and try to avoid reserved words too 🙂

---

<div class="post-metadata">

**Author:** ![malvadomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@malvadomx](https://www.ruby-forum.com/u/malvadomx)\
**Post date:** [January 12, 2011, 11:16am UTC](https://www.ruby-forum.com/t/has-many-through-one/201650/5 "2011-01-12T11:16:36Z")

</div>

On 12 January 2011 09:50, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> …or create a delegate method in user to return the profile’s files:
> 
> class User \< AR  
> has\_one :profile  
> def files  
> self.profile ? self.profile.files :   
> end  
> end

Or just put the profile data in the user table of course.

Colin

---

<div class="post-metadata">

**Author:** ![malvadomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@malvadomx](https://www.ruby-forum.com/u/malvadomx)\
**Post date:** [January 12, 2011, 11:31am UTC](https://www.ruby-forum.com/t/has-many-through-one/201650/6 "2011-01-12T11:31:44Z")

</div>

On 12 January 2011 10:14, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Or just put the profile data in the user table of course.

yebbut… encapsulation _is_ a “good thing”… and you wouldn’t want  
to encourage the OP to denormalise for the sake of it. It is nice to  
have classes responsible for their own stuff. I like to keep the User  
model of my own for just authentication/essential info, and delegate  
addresses, telephones, hobbies, etc, to their own classes… horses  
for courses 🙂

---

<div class="post-metadata">

**Author:** ![malvadomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@malvadomx](https://www.ruby-forum.com/u/malvadomx)\
**Post date:** [January 12, 2011, 11:55am UTC](https://www.ruby-forum.com/t/has-many-through-one/201650/7 "2011-01-12T11:55:45Z")

</div>

On 12 January 2011 10:30, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 12 January 2011 10:14, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Or just put the profile data in the user table of course.
> 
> yebbut… encapsulation _is_ a “good thing”… and you wouldn’t want  
> to encourage the OP to denormalise for the sake of it. It is nice to  
> have classes responsible for their own stuff. I like to keep the User  
> model of my own for just authentication/essential info, and delegate  
> addresses, telephones, hobbies, etc, to their own classes… horses  
> for courses 🙂

I agree about the encapsulation, it all depends on rest of the app  
which way to go. However I don’t think your use of the word  
denormalise is correct. The db would not be denormalised by moving  
stuff into the user table. There would be no replication of data as  
it is a has\_one relationship. It would not be appropriate for things  
such as hobbies which would likely be a habtm relationship.

Colin

---

<div class="post-metadata">

**Author:** ![malvadomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@malvadomx](https://www.ruby-forum.com/u/malvadomx)\
**Post date:** [January 12, 2011, 12:31pm UTC](https://www.ruby-forum.com/t/has-many-through-one/201650/8 "2011-01-12T12:31:45Z")

</div>

On 12 January 2011 10:44, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> However I don’t think your use of the word  
> denormalise is correct.

yeah… wrong term… sorry for any confusion. Encapsulation and  
separation of responsibility is more the gist of why I’d keep the User  
and Profile stuff apart. All personal preference and project  
requirements decisions though…
