# Getting column value from lookup table in HABTM relationship

**URL:** <https://www.ruby-forum.com/t/getting-column-value-from-lookup-table-in-habtm-relationship/59844>\
**Category:** Rails\
**Created:** [May 7, 2006, 1:48pm UTC](https://www.ruby-forum.com/t/getting-column-value-from-lookup-table-in-habtm-relationship/59844 "2006-05-07T13:48:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 7, 2006, 1:48pm UTC](https://www.ruby-forum.com/t/getting-column-value-from-lookup-table-in-habtm-relationship/59844/1 "2006-05-07T13:48:54Z")

</div>

I have a HABTM relationship between my agents and listings tables. Each  
listing can have many agents and each agent can have many listings. In  
the agents\_listings table I have a column called ‘is\_primary\_agent’  
which denotes if the agent is responsible for the listing - only one  
agent can be primary.

At the moment I use this in my listing\_controller’s view action:  
@listing = Listing.find(@params[:id], :include =\> [ :agents,  
:listing\_images, :listing\_pdfs ] )

What is the best, most efficient way to have this attribute appear  
against my agents?

TIA,

Matt.

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 7, 2006, 3:24pm UTC](https://www.ruby-forum.com/t/getting-column-value-from-lookup-table-in-habtm-relationship/59844/2 "2006-05-07T15:24:28Z")

</div>

It sounds like you need to store the primary agent ids in the listings  
table, rather than in the join table.

Matthew L. wrote:

> I have a HABTM relationship between my agents and listings tables. Each  
> listing can have many agents and each agent can have many listings. In  
> the agents\_listings table I have a column called ‘is\_primary\_agent’  
> which denotes if the agent is responsible for the listing - only one  
> agent can be primary.
> 
> At the moment I use this in my listing\_controller’s view action:  
> @listing = Listing.find(@params[:id], :include =\> [ :agents,  
> :listing\_images, :listing\_pdfs ] )
> 
> What is the best, most efficient way to have this attribute appear  
> against my agents?
> 
> TIA,
> 
> Matt.

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 7, 2006, 3:43pm UTC](https://www.ruby-forum.com/t/getting-column-value-from-lookup-table-in-habtm-relationship/59844/3 "2006-05-07T15:43:25Z")

</div>

D’oh!

Sorted now, Thanks!

M.

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 10, 2006, 3:02am UTC](https://www.ruby-forum.com/t/getting-column-value-from-lookup-table-in-habtm-relationship/59844/4 "2006-05-10T03:02:43Z")

</div>

On 5/10/06, Eden B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If anyone else is following along, I find myself wondering if putting  
> something like is\_primary\_agent in the join model is the best approach. The  
> other option of course would be to just use a habtm and add an attribute to  
> the listing model called primary\_agent\_id. Any suggestions?

Definitely preferable to have the primary\_agent\_id on the listing model.  
In the DB there’d be only one record to change when the primary agent  
changes (as opposed to two if using a flag) - and there’s no chance of  
ending up with multiple agents flagged as primary. The trade off though  
is  
that you have have to hit both the listing and the agent table to  
determine  
the primary agent for the listing.

cheers,  
Ben

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 10, 2006, 2:44am UTC](https://www.ruby-forum.com/t/getting-column-value-from-lookup-table-in-habtm-relationship/59844/5 "2006-05-10T02:44:09Z")

</div>

I am not sure I understand your question. Do you want to show the  
primary  
agent in your list of listings? It seems like you are using a :through  
join  
model. Is this correct?

Assuming that your join model is called Representations, then couldn’t  
you  
just do the following:

# listing\_controller

@primary\_agent = @listing.agents.find\_by\_is\_primary\_agent(true)

# view

Primary Agent: \<%= @primary\_agent.name %\>

I haven’t tried this, but it seems like the right approach.

If anyone else is following along, I find myself wondering if putting  
something like is\_primary\_agent in the join model is the best approach.  
The  
other option of course would be to just use a habtm and add an attribute  
to  
the listing model called primary\_agent\_id. Any suggestions?
