# Access values with has\_and\_belongs\_to\_many assocciation

**URL:** <https://discuss.rubyonrails.org/t/access-values-with-has-and-belongs-to-many-assocciation/43089>\
**Category:** rubyonrails-talk\
**Created:** [June 1, 2009, 9:01pm UTC](https://discuss.rubyonrails.org/t/access-values-with-has-and-belongs-to-many-assocciation/43089 "2009-06-01T21:01:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Konstantinos\_Loupos](https://avatars.discourse-cdn.com/v4/letter/k/96bed5/32.png) [@Konstantinos\_Loupos](https://discuss.rubyonrails.org/u/Konstantinos_Loupos)\
**Post date:** [June 1, 2009, 9:01pm UTC](https://discuss.rubyonrails.org/t/access-values-with-has-and-belongs-to-many-assocciation/43089/1 "2009-06-01T21:01:33Z")

</div>

Hi! I have 2 models author and product.

class Author \< ActiveRecord::Base &nbsp;&nbsp;has\_and\_belongs\_to\_many :products end

class Product \< ActiveRecord::Base &nbsp;&nbsp;has\_and\_belongs\_to\_many :authors end

I have also created the migration for the jointable authors\_products with the keys author\_id and product\_id.

Now, i suppose that when i have eg an author with author\_id = 1 and a product\_id=4 in the join table, i should be able to access the product title of that product and the author name of my author.

How can i access these values???

Thanx in advance for your time!

---

<div class="post-metadata">

**Author:** ![Brian10](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@Brian10](https://discuss.rubyonrails.org/u/Brian10)\
**Post date:** [June 2, 2009, 12:10am UTC](https://discuss.rubyonrails.org/t/access-values-with-has-and-belongs-to-many-assocciation/43089/2 "2009-06-02T00:10:39Z")

</div>

The association adds some helper attributes to your model. If you have an instance of an author you can use the products attribute to get all of the associated products in an array:

my\_author = Author.find(1) my\_author.products

---

<div class="post-metadata">

**Author:** ![Konstantinos\_Loupos](https://avatars.discourse-cdn.com/v4/letter/k/96bed5/32.png) [@Konstantinos\_Loupos](https://discuss.rubyonrails.org/u/Konstantinos_Loupos)\
**Post date:** [June 3, 2009, 7:30pm UTC](https://discuss.rubyonrails.org/t/access-values-with-has-and-belongs-to-many-assocciation/43089/3 "2009-06-03T19:30:38Z")

</div>

Thanx Brian. So if i want to see the product title i should write my\_author.products.title??? Or i should use something else? Cause i've tried it but i get nothing

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 3, 2009, 9:12pm UTC](https://discuss.rubyonrails.org/t/access-values-with-has-and-belongs-to-many-assocciation/43089/4 "2009-06-03T21:12:18Z")

</div>

my\_author.products gives you an array of all the products for that author, each of which has a title, so for example my\_author.products[0].title will give you the title of the first one

Colin

---

<div class="post-metadata">

**Author:** ![Konstantinos\_Loupos](https://avatars.discourse-cdn.com/v4/letter/k/96bed5/32.png) [@Konstantinos\_Loupos](https://discuss.rubyonrails.org/u/Konstantinos_Loupos)\
**Post date:** [June 6, 2009, 1:18pm UTC](https://discuss.rubyonrails.org/t/access-values-with-has-and-belongs-to-many-assocciation/43089/5 "2009-06-06T13:18:17Z")

</div>

Thanks! Your help was really usefull!

---

<div class="post-metadata">

**Author:** ![Konstantinos\_Loupos](https://avatars.discourse-cdn.com/v4/letter/k/96bed5/32.png) [@Konstantinos\_Loupos](https://discuss.rubyonrails.org/u/Konstantinos_Loupos)\
**Post date:** [June 6, 2009, 1:24pm UTC](https://discuss.rubyonrails.org/t/access-values-with-has-and-belongs-to-many-assocciation/43089/6 "2009-06-06T13:24:34Z")

</div>

Sijo,

map works great! but is there any other way showing the title but not using map? I am curious because i’ve been searching for this but i found nothing.

Thank u Kostas

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 6, 2009, 1:50pm UTC](https://discuss.rubyonrails.org/t/access-values-with-has-and-belongs-to-many-assocciation/43089/7 "2009-06-06T13:50:51Z")

</div>

> Sijo,
> 
> map works great! but is there any other way showing the title but not using map? I am curious because i've been searching for this but i found nothing.

What exactly do you mean by 'showing the title'. The map function returns an array containing the titles of all the products. What is it that you are trying to achieve?

Colin

---

<div class="post-metadata">

**Author:** ![Konstantinos\_Loupos](https://avatars.discourse-cdn.com/v4/letter/k/96bed5/32.png) [@Konstantinos\_Loupos](https://discuss.rubyonrails.org/u/Konstantinos_Loupos)\
**Post date:** [June 8, 2009, 4:29pm UTC](https://discuss.rubyonrails.org/t/access-values-with-has-and-belongs-to-many-assocciation/43089/8 "2009-06-08T16:29:29Z")

</div>

Thank u all. Colin sorry for my previous question i found the sollution i wanted. There is something else i want to do. I calculate the size of this array. If the size is less than 2, i want to see the name of the author, and if there are more than 2 authors i want to see both names with a party-comma between them. here is the code so far:

\<% if (product.authors.map(&:name).size) \< 2 %\> \<%= “Author: Here i should see the name of the author” %\> \<% else %\> \<%= “Authors: Here i should see all the authors name separated with a party-comma.”%\> \<% end %\>

How can i do that? Thank u for your time again! Kostas

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 8, 2009, 8:47pm UTC](https://discuss.rubyonrails.org/t/access-values-with-has-and-belongs-to-many-assocciation/43089/9 "2009-06-08T20:47:59Z")

</div>

No need to test for the special case of only one element, look up the docs for Array#join and try something like product.authors.map(&:name).join(',') I am not sure what a party-comma is. The only thing this won't do is the s on the end of Author in the case of multiple. So you may need to do the test if you really need this. If so then product.authors.size \< 2 will do, no need to convert it to an array of names before checking the size. There is one thing to watch out for though in both cases, suppose product has no authors at all, then product.authors will be nil and you would get an exception trying to call map or size on nil. In this case you may want to display something else. One way to tidy this up so you don't end up with messy code in your view would be to add a method to the product model that returns the string you wish to see displayed and handles the nil case internally then the view would just contain \<%= product.authors\_text %\> where authors\_text is product model method.

I think possibly you could usefully work through some basic tutorials on Ruby to get you going.

Colin
