# Having trouble setting up model relationships

**URL:** <https://www.ruby-forum.com/t/having-trouble-setting-up-model-relationships/146655>\
**Category:** Rails\
**Created:** [August 23, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/having-trouble-setting-up-model-relationships/146655 "2008-08-23T19:18:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [August 23, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/having-trouble-setting-up-model-relationships/146655/1 "2008-08-23T19:18:11Z")

</div>

I’m working on a new project, and I’m stuck on making some seemingly  
simple model associations work. I have the following models which I’m  
trying to get to play nice:

- entry  
belongs\_to :users  
has\_and\_belongs\_to\_many :categories
- category  
has\_and\_belongs\_to\_many :entries
- categories\_entries (the join model)

As I loop through entries in the view, i want to be able to be able to  
list any associated category name next to the entry name. So in the  
view I’d like to write, entry.category.name to access the category  
name, though when i try that, I get “undefined method `category’ for  
#Entry:0x256c0dc.”

How would I set this up correctly? I’ve blown apart the associations  
many times at this point and I haven’t stumbled upon the correct way  
to do this. Any help would be greatly appreciated.

Thanks,  
-A

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [August 23, 2008, 7:25pm UTC](https://www.ruby-forum.com/t/having-trouble-setting-up-model-relationships/146655/2 "2008-08-23T19:25:12Z")

</div>

ressister wrote:

> I’m working on a new project, and I’m stuck on making some seemingly  
> simple model associations work. I have the following models which I’m  
> trying to get to play nice:
> 
> - entry  
> belongs\_to :users  
> has\_and\_belongs\_to\_many :categories
> - category  
> has\_and\_belongs\_to\_many :entries
> - categories\_entries (the join model)

Firstly, if you write unit tests for this, and if you fill your  
entries.yml and  
categories.yml files with sample fixtures that illustrate your project,  
you can  
start with simple test cases that explore how the relations work.

> As I loop through entries in the view, i want to be able to be able to  
> list any associated category name next to the entry name. So in the  
> view I’d like to write, entry.category.name to access the category  
> name, though when i try that, I get “undefined method `category’ for  
> #Entry:0x256c0dc.”

That is exactly because an entry has _many_ categories. So name them  
all:

entry.categories.map(&:name).join(’, ')

Write that into a unit test, and then into your view. If you go with it,  
refactor it into a reusable method: entry.category\_names.

However, if your users don’t want to see extra category names, you must  
find  
some way to distinguish them:

entry.categories.find\_by\_distinguishing\_field(foo).name

–  
Phlip

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [August 24, 2008, 5:41am UTC](https://www.ruby-forum.com/t/having-trouble-setting-up-model-relationships/146655/3 "2008-08-24T05:41:18Z")

</div>

Hi Philip, thanks for the reply. I’m just trying to get the  
association working - to be able to link through the join table so I  
can access the category for an entry. I’ve decided that I only want  
an entry to belong to one category. Still, I’m having trouble.  
Here’s where I am right now:

- entry

- category

- entry\_category (I renamed this join table)

What’s missing? I’ve tried in the entry model to use  
has\_one :category, :through =\> :entry\_category but i got an error  
about not being able to find the key(s). Let me know if you have any  
additional thoughts.

Thanks again for your help.

-A
