Hi folks, Newbie with ruby on rails but very excited by the features provided by the framework ;) Suppose I''ve got a table ''Company'' and a table ''Category'' Company belongs to a category but I will retrieve the Catagory in the Company list? It seems that I don''t have the basic ruby wau of thinking to get this easily. Can someone help? Many thanks in advance Stef -- Posted via http://www.ruby-forum.com/.
hi, Just to note, your tables should be a plural version of the models: companies categories Your companies table will have ''category_id'' that references the ''id'' field in the categories table. You models will then be set up to reflect this relationship: class Company < ActiveRecord::Base belongs_to :category end class Category < ActiveRecord::Base has_many :companies end You''ll then be able to retrive company and find it''s assigned category: # get company (assuming you have a company with id=1) company = Company.find(1) # get category name category_name = company.category.name For a great introduction to all this, the Agile book (http://www.pragmaticprogrammer.com/titles/rails/index.html) is highly recommended. Hope that helps, Steve Stephane Gauthier wrote:> Hi folks, > > Newbie with ruby on rails but very excited by the features provided by > the framework ;) > > Suppose I''ve got a table ''Company'' and a table ''Category'' Company > belongs to a category but I will retrieve the Catagory in the Company > list? > It seems that I don''t have the basic ruby wau of thinking to get this > easily. > > Can someone help? > > Many thanks in advance > > Stef >
Stephane Gauthier wrote:> Hi folks, > > Newbie with ruby on rails but very excited by the features provided by > the framework ;) > > Suppose I''ve got a table ''Company'' and a table ''Category'' Company > belongs to a category but I will retrieve the Catagory in the Company > list? > It seems that I don''t have the basic ruby wau of thinking to get this > easily. > > Can someone help? > > Many thanks in advance > > StefOk, found it. company.category.name ... so simple! -- Posted via http://www.ruby-forum.com/.
Stephen Bartholomew wrote:> hi, > > Just to note, your tables should be a plural version of the models: > companies > categories > > Your companies table will have ''category_id'' that references the ''id'' > field in the categories table. > > You models will then be set up to reflect this relationship: > > class Company < ActiveRecord::Base > belongs_to :category > end > > class Category < ActiveRecord::Base > has_many :companies > end > > You''ll then be able to retrive company and find it''s assigned category: > > # get company (assuming you have a company with id=1) > company = Company.find(1) > > # get category name > category_name = company.category.name > > For a great introduction to all this, the Agile book > (http://www.pragmaticprogrammer.com/titles/rails/index.html) is highly > recommended. > > Hope that helps, > > SteveYes, I already got this. thanks -- Posted via http://www.ruby-forum.com/.