Mitchell Gould
2009-Oct-06 12:32 UTC
How do I access a child table/model from the parent in sql s
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) -- Posted via http://www.ruby-forum.com/.
Alexei Spirit
2009-Oct-06 12:43 UTC
Re: How do I access a child table/model from the parent in sql s
Something like this: Manufacturer.all :joins => :description, :conditions => {:descriptions => {:locale => I18n.locale}} -- Posted via http://www.ruby-forum.com/.
Alexei Spirit
2009-Oct-06 12:50 UTC
Re: How do I access a child table/model from the parent in sql s
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}} -- Posted via http://www.ruby-forum.com/.
Mitchell Gould
2009-Oct-06 16:19 UTC
Re: How do I access a child table/model from the parent in sql s
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. -- Posted via http://www.ruby-forum.com/.
Mitchell Gould
2009-Oct-06 16:37 UTC
Re: How do I access a child table/model from the parent in sql s
Mitchell Gould 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 -- Posted via http://www.ruby-forum.com/.
Alexei Spirit
2009-Oct-06 17:04 UTC
Re: How do I access a child table/model from the parent in sql s
> 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 advanceWith 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 -- Posted via http://www.ruby-forum.com/.
Alexei Spirit
2009-Oct-06 17:06 UTC
Re: How do I access a child table/model from the parent in sql s
I suppose this would very helpfull for u: http://guides.rubyonrails.org/active_record_querying.html -- Posted via http://www.ruby-forum.com/.
Alexei Spirit
2009-Oct-06 17:07 UTC
Re: How do I access a child table/model from the parent in sql s
I suppose this would be very helpfull for u: http://guides.rubyonrails.org/active_record_querying.html -- Posted via http://www.ruby-forum.com/.
Mitchell Gould
2009-Oct-07 00:41 UTC
Re: How do I access a child table/model from the parent in sql s
Alexei Spirit wrote:> I suppose this would be very helpfull for u: > http://guides.rubyonrails.org/active_record_querying.htmlYes Alexei that is the perfect guide. Thanks again. -- Posted via http://www.ruby-forum.com/.