# How do I access a child table/model from the parent in sql s

**URL:** <https://www.ruby-forum.com/t/how-do-i-access-a-child-table-model-from-the-parent-in-sql-s/176809>\
**Category:** Rails\
**Created:** [October 6, 2009, 2:32pm UTC](https://www.ruby-forum.com/t/how-do-i-access-a-child-table-model-from-the-parent-in-sql-s/176809 "2009-10-06T14:32:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [October 6, 2009, 2:32pm UTC](https://www.ruby-forum.com/t/how-do-i-access-a-child-table-model-from-the-parent-in-sql-s/176809/1 "2009-10-06T14:32:31Z")

</div>

I am trying to use the fourth normal form for my applications so that i  
can get the information in the appropriate language.

I have a table called manufacturers. I have another called descriptions.  
The descriptions are in two languages and each description belongs to a  
particular language. So two records in the Descriptions table one in  
English and One in French for each manufacturer. The descriptions table  
has the manufactturers id as a foreign key.

I have followed this for my Rails project but am stuck.

I have two models.

One is called Manufacturer.  
One is called Description.

Manufacturer  
has\_many :descriptions

Description  
belongs\_to :manufacturer

In the manufacturers model I have the following method to look up all of  
the manufacturers for the given locale.

def self.find\_all  
find(:all, :order =\> “name”, :conditions =\> {:locale =\> I18n.locale})  
end

This produces the following error

Mysql::Error: Unknown ‘manufacturers.locale’ in ‘where clause’: SELECT \*  
FROM ‘manufacturers’ where (‘manufacturers’.‘locale’ = ‘en’)

how can I fix the sql statement in Rails?

Thank you in advance

Last edited by mitch\_newbie (Today 07:24:24)

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [October 6, 2009, 2:43pm UTC](https://www.ruby-forum.com/t/how-do-i-access-a-child-table-model-from-the-parent-in-sql-s/176809/2 "2009-10-06T14:43:52Z")

</div>

Something like this:

Manufacturer.all :joins =\> :description, :conditions =\> {:descriptions  
=\> {:locale =\> I18n.locale}}

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [October 6, 2009, 2:50pm UTC](https://www.ruby-forum.com/t/how-do-i-access-a-child-table-model-from-the-parent-in-sql-s/176809/3 "2009-10-06T14:50:23Z")

</div>

Alexei Spirit wrote:

> Something like this:
> 
> Manufacturer.all :joins =\> :description, :conditions =\> {:descriptions  
> =\> {:locale =\> I18n.locale}}

Version with :order

Manufacturer.all :joins =\> :description, :order =\> :name, :conditions =\>  
{:descriptions =\> {:locale =\> I18n.locale}}

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [October 6, 2009, 6:19pm UTC](https://www.ruby-forum.com/t/how-do-i-access-a-child-table-model-from-the-parent-in-sql-s/176809/4 "2009-10-06T18:19:26Z")

</div>

Alexei Spirit wrote:

> Alexei Spirit wrote:
> 
> > Something like this:
> > 
> > Manufacturer.all :joins =\> :description, :conditions =\> {:descriptions  
> > =\> {:locale =\> I18n.locale}}
> 
> Version with :order
> 
> Manufacturer.all :joins =\> :description, :order =\> :name, :conditions =\>  
> {:descriptions =\> {:locale =\> I18n.locale}}

Thank you so much.

I did this based upon what you wrote above and it works great.

find(:all, :joins =\> :descriptions, :order =\> :name, :conditions =\>  
{:descriptions =\> {:locale =\> I18n.locale}})

Thanks again.

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [October 6, 2009, 7:04pm UTC](https://www.ruby-forum.com/t/how-do-i-access-a-child-table-model-from-the-parent-in-sql-s/176809/5 "2009-10-06T19:04:25Z")

</div>

> One last question.  
> I need the description for the manufacturer that is found in the table  
> descriptions. How do I get this columns info and how do I access it in  
> the view?
> 
> Thank you in advance

With this one:

> find(:all, :joins =\> :descriptions, :order =\> :name, :conditions =\>  
> {:descriptions =\> {:locale =\> I18n.locale}})  
> since you descript your associations you can get manufacturers with all  
> associated models, then you can use it anywhere

Like this one for example:

@manufacturers = Manufacturer.all :joins =\> :description, :order =\>  
:name, :conditions =\> {:descriptions =\> {:locale =\> I18n.locale}}

@manufacturers.each do |manufacturer|  
puts manufacturer.description.description  
end

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [October 6, 2009, 6:37pm UTC](https://www.ruby-forum.com/t/how-do-i-access-a-child-table-model-from-the-parent-in-sql-s/176809/6 "2009-10-06T18:37:00Z")

</div>

Mitchell G. wrote:

> Alexei Spirit wrote:
> 
> > Alexei Spirit wrote:
> > 
> > > Something like this:
> > > 
> > > Manufacturer.all :joins =\> :description, :conditions =\> {:descriptions  
> > > =\> {:locale =\> I18n.locale}}
> > 
> > Version with :order
> > 
> > Manufacturer.all :joins =\> :description, :order =\> :name, :conditions =\>  
> > {:descriptions =\> {:locale =\> I18n.locale}}
> 
> Thank you so much.
> 
> I did this based upon what you wrote above and it works great.
> 
> find(:all, :joins =\> :descriptions, :order =\> :name, :conditions =\>  
> {:descriptions =\> {:locale =\> I18n.locale}})
> 
> Thanks again.

One last question.  
I need the description for the manufacturer that is found in the table  
descriptions. How do I get this columns info and how do I access it in  
the view?

Thank you in advance

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [October 6, 2009, 7:06pm UTC](https://www.ruby-forum.com/t/how-do-i-access-a-child-table-model-from-the-parent-in-sql-s/176809/7 "2009-10-06T19:06:55Z")

</div>

I suppose this would very helpfull for u:

> **[Active Record Query Interface — Ruby on Rails Guides](https://guides.rubyonrails.org/active_record_querying.html)**
>
> Active Record Query InterfaceThis guide covers different ways to retrieve data from the database using Active Record.After reading this guide, you will know: How to find records using a variety of methods and conditions. How to specify the order,...

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [October 6, 2009, 7:07pm UTC](https://www.ruby-forum.com/t/how-do-i-access-a-child-table-model-from-the-parent-in-sql-s/176809/8 "2009-10-06T19:07:05Z")

</div>

I suppose this would be very helpfull for u:

> **[Active Record Query Interface — Ruby on Rails Guides](https://guides.rubyonrails.org/active_record_querying.html)**
>
> Active Record Query InterfaceThis guide covers different ways to retrieve data from the database using Active Record.After reading this guide, you will know: How to find records using a variety of methods and conditions. How to specify the order,...

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [October 7, 2009, 2:41am UTC](https://www.ruby-forum.com/t/how-do-i-access-a-child-table-model-from-the-parent-in-sql-s/176809/9 "2009-10-07T02:41:32Z")

</div>

Alexei Spirit wrote:

> I suppose this would be very helpfull for u:  
> [Active Record Query Interface — Ruby on Rails Guides](http://guides.rubyonrails.org/active_record_querying.html)

Yes Alexei that is the perfect guide. Thanks again.
