Topic: relations

Hi,

I'm new to rails and got some problems how i should design my models and relations the "rails way".

I've got the following Models:
Vendor
Service
Product
Certification
Place

relations:
Vendor habtm  Services
Vendor habtm  Products

Service of Vendor has_many Certifications
Service of Vendor has_many Places

Product of Vendor has_many Certifications
Product of Vendor has_many Places


... sorry for the bad english.. my native language is italian.

anyway, the question is... how can I implement the "Right" model with its relations in a proper way?
do can use polymorphic associations?

cheers,
salvo

Last edited by salvo (2007-03-22 14:06:28)

Re: relations

dovete avere polymorphic relationship per Certification et Place.

dovete un altro relationship come 'Contracts' (o altro nome) fra Vendor et Service.  Vendor has_many :contracts.  Service has_many :contracts.  Vendor has_many :services, :through => :contracts.  Services has_many :vendors, :through => :contracts
Anche fra Vendor et Product ('Productions'? )

Scusa per il mio italiano, ho studiato per un anno solo a l'universita. (Whoohoo! Ho non dimenticato tutti!)

Re: relations

Ecco, i modeli:

class Vendor
  has_many :contracts
  has_many :services, :through => :contracts
end
class Contract # fra Vendor et Service
  belongs_to :service
  belongs_to :vendor
end
class Production # fra Vendor et Product
  belongs_to :vendors
  belongs_to :products
end
class Service
  has_many :contracts
  has_many :vendors, :through => :contracts
  has_many :places, :as => :placeable
  has_many :certifications, :as => :certifiable
end
class Product
  has_many :productions
  has_many :vendors, :through => :productions
  has_many :places, :as => :placeable
  has_many :certifications, :as => :certifiable
end
class Certification
  belongs_to :certifiable, :polymorphic => true
end
class Place
  belongs_to :placeable, :polymorphic => true
end

Re: relations

thanks for the answer, your italian is not badly.
If I have instead this scene:

class Vendor < ActiveRecord::Base
    has_many :vendor_categories
    has_many :categories, :through => :vendor_categories
    has_many :vendor_localities
    has_many :localities, :through => :vendor_localities
    has_many :vendor_certifications
    has_many :certifications, :through => :vendor_certifications
end

class Category < ActiveRecord::Base
    has_many :vendor_categories
    has_many :vendors, :through => :vendor_categories
end

class VendorCategory < ActiveRecord::Base
    belongs_to :vendor
    belongs_to :category
end

class Locality < ActiveRecord::Base
    has_many :vendor_localities
end

class VendorLocality < ActiveRecord::Base
    belongs_to :vendor
    belongs_to :locality
end

class Certification < ActiveRecord::Base
    has_many :vendor_certifications
end

class VendorCertification < ActiveRecord::Base
    belongs_to :vendor
    belongs_to :certification
end
 
---------------------
   
Which methods find i can use in order to search the vendors for
Locality and Category and Certification ?

cheers

Last edited by salvo (2007-03-23 10:24:35)

Re: relations

This is the best way that I know of:

# Vendors that have the 'First Category' associated with them:
Vendor.find(:all, :conditions => ['categories.name = ?', 'First Category'], :include => :categories)

#Vendors in the area of Napoli:
Vendor.find(:all, :conditions => ['localities.name = ?', 'Napoli'], :include => :localities)

#Vendors in the area of Napoli or Polermo:
Vendor.find(:all, :conditions => ['localities.name in (?, ?)', 'Napoli', 'Polermo'], :include => :localities)