# ThroughAssociations

**URL:** <https://www.ruby-forum.com/t/throughassociations/160086>\
**Category:** Rails\
**Created:** [February 11, 2009, 5:21pm UTC](https://www.ruby-forum.com/t/throughassociations/160086 "2009-02-11T17:21:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [February 11, 2009, 5:21pm UTC](https://www.ruby-forum.com/t/throughassociations/160086/1 "2009-02-11T17:21:36Z")

</div>

There is example:  
[http://wiki.rubyonrails.org/rails/pages/ThroughAssociations](http://wiki.rubyonrails.org/rails/pages/ThroughAssociations)

Example

Catalogue has\_many :catalogue\_items  
CatalogueItems belongs\_to :catalogue; belongs\_to :product  
Product has\_many :catalogue\_items

So how I can get:

catalogues.name  
catalogue\_items.position  
products.name  
products. price  
WHERE  
catalogues.name LIKE “xxx%”

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [February 11, 2009, 6:15pm UTC](https://www.ruby-forum.com/t/throughassociations/160086/2 "2009-02-11T18:15:46Z")

</div>

James B. wrote:

> There is example:  
> [http://wiki.rubyonrails.org/rails/pages/ThroughAssociations](http://wiki.rubyonrails.org/rails/pages/ThroughAssociations)
> 
> Example
> 
> Catalogue has\_many :catalogue\_items  
> CatalogueItems belongs\_to :catalogue; belongs\_to :product  
> Product has\_many :catalogue\_items
> 
> So how I can get:
> 
> catalogues.name  
> catalogue\_items.position  
> products.name  
> products. price  
> WHERE  
> catalogues.name LIKE “xxx%”

First of all, amend your association in Catalogue as follows - this will  
mean that the catalogue’s items (and therefore products) will  
automatically be ordered by position.

Catalogue  
has\_many :catalogue\_items, :order =\> “position”

Then, in the controller:

#do an include to eager load the relevant associations.  
@catalogues = Catalogue.find(:all, :conditions =\> [“name like ?”,  
“#{a\_variable}%”], :include =\> {:catalogue\_items =\> :products})

And then, for example, in the view:

\<% @catalogues.each do |catalogue| %\>

Catalogue:\<%= catalogue.name %\>  
 \<% catalogue.catalogue\_items.each do |item| %\> 
\<%= item.position %\>: \<%= item.product.name %\>: \<%= item.product.price %\>
 \<% end %\> 
\<% end %\>

Obviously you can put your own html around this, this is just a simple  
example.

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [February 11, 2009, 7:14pm UTC](https://www.ruby-forum.com/t/throughassociations/160086/3 "2009-02-11T19:14:49Z")

</div>

> Catalogue  
> has\_many :catalogue\_items, :order =\> “position”

Thanks, but if I want sot like:

:order =\> “catalogues.name!=’#{@name}’, catalogues.name, products.name”

Where @name is serarch word.

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [February 11, 2009, 7:54pm UTC](https://www.ruby-forum.com/t/throughassociations/160086/4 "2009-02-11T19:54:35Z")

</div>

James B. wrote:

> > Catalogue  
> > has\_many :catalogue\_items, :order =\> “position”
> 
> Thanks, but if I want sot like:
> 
> :order =\> “catalogues.name!=’#{@name}’, catalogues.name, products.name”
> 
> Where @name is serarch word.

That doesn’t make sense though - you’re trying to shove a search  
condition into the order option, and you’ve got all sorts of syntax  
errors going on.

Can you explain, in english as opposed to code, what it is that you  
ultimately want to do? Neither of the things you’ve asked for have  
really made sense so i’m just guessing. You’re asking how to do a  
particular bit of code but i don’t think the code you’re looking for is  
actually the code you need to do the job, if you know what i mean.

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [February 11, 2009, 8:16pm UTC](https://www.ruby-forum.com/t/throughassociations/160086/5 "2009-02-11T20:16:40Z")

</div>

> Can you explain, in english as opposed to code, what it is that you  
> ultimately want to do?

Sorry.

Search “way” I use is: match(catalogues.name) against “xxx”

And “order by” is (catalogues.name, products.name)

But first where catalogues.name == “xxx”

Example

If catalogues.names are: “aaa xxx”, “xxx”, “rrr xxx”  
match(catalogues.name) against “xxx” will found them all and default  
:order =\> “catalogues.name” will sort them like this:

1 “aaa xxx”  
2 “rrr xxx”  
3 “xxx”

But if I use :order =\> “catalogues.name!=’#{@name}’, catalogues.name”  
order will be:

1 “xxx”  
2 “aaa xxx”  
3 “rrr xxx”

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [February 11, 2009, 9:09pm UTC](https://www.ruby-forum.com/t/throughassociations/160086/6 "2009-02-11T21:09:24Z")

</div>

That’s not any clearer - you’re still just giving code examples. Like,  
with the two examples there, you don’t say if either of them gives the  
results you want, and if they don’t why not.

This:

:order =\> “catalogues.name!=’#{@name}’, catalogues.name”

doesn’t even make sense to me - i’m amazed that you get any result back  
at all from it.

Say something like

“I want to find all catalogues matching a given name, order them by name  
and then list their products”

or whatever it is you’re actually trying to do.

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [February 11, 2009, 9:35pm UTC](https://www.ruby-forum.com/t/throughassociations/160086/7 "2009-02-11T21:35:42Z")

</div>

It is hard explain without example.

I need Full-Text Search

SELECT \* FROM products WHERE MATCH(name) AGAINST(‘lamp’) ORDER BY name

Returns:

“alarm lamp”  
“lamp”  
“trouble lamp”  
“wall lamp”

But: SELECT \* FROM products WHERE MATCH(name) AGAINST(‘lamp’) ORDER BY  
name!=‘lamp’, name

Returns:

“lamp”  
“alarm lamp”  
“trouble lamp”  
“wall lamp”

Note difference, first result is “lamp” exactly same as search word was,  
and after that comes “alarm lamp” and so on…

So now we only add: catalogues.name, catalogue\_items.position,  
products.name, products.price to results…
