parent = child.Parent <<-- that was i
nil <<-- nil with rails 2.0.2
It depends on the definition of your services table. The standard way
is for the services table to contain a column called type_service_id and
then use:
class TypeService < ActiveRecord::Base
has_many :services
end
class Service < ActiveRecord::Base
belongs_to :type_service
end
s = Service.find(1)
name_type_service = s.type_service.name
The arguments to “has_many” and “belongs_to” become methods in the class
which return the connected object(s) from the other class. The
“belongs_to” method returns a single object and the “has_many” method
returns an array of objects, so similarly:
t = TypeService.find(1)
services_for_t = t.services
Here services_for_t is now a (possibly empty) array of Service objects.
If the column in your services table is called something else, then you
need to specify it as the foreign key in the relation, so if this column
is typeservice_id, for example, use:
class TypeService < ActiveRecord::Base
has_many :services, :foreign_key => :typeservice_id
end
class Service < ActiveRecord::Base
belongs_to :type_service, :foreign_key => :typeservice_id
end