Forum: Ruby on Rails Find By ID

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Stephane G. (Guest)
on 2006-05-11 12:36
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 G. (Guest)
on 2006-05-11 12:48
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

Ok, found it.

company.category.name ... so simple!
Stephen B. (Guest)
on 2006-05-11 12:48
(Received via mailing list)
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 G. (Guest)
on 2006-05-11 17:12
Stephen B. 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,
>
> Steve


Yes, I already got this.

thanks
This topic is locked and can not be replied to.