Salut,
Une partie de mes tables ne respecte pas le schéma de Ruby. J’ai
donc indiquer à la main mes clé primaires
Table & Model Pays
class Pay < ActiveRecord::Base
set_table_name “Expay_Pays”
set_primary_key “CodePays”
has_many :zones, :class_name => “ZonesGraphique”, :foreign_key =>
“IDZoneGeographique”
has_many :code_postaux, :class_name =>
“CodesPostaux”, :foreign_key => “IDCodePostal”
end
create_table :Expay_Pays, :id => false, :primary_key =>
‘CodePays’ do |t|
t.column :CodePays, :string
t.column :LibelleFr, :string
t.column :EstActif, :integer
t.column :NomZone, :string
end
Table & Model ZonesGeographiques
class ZonesGraphique < ActiveRecord::Base
set_primary_key “IDZoneGeographique”
set_table_name “Expay_ZonesGraphiques”
belongs_to :pay, :class_name => “Pay”, :foreign_key => “CodePays”
has_many :distributeurs, :class_name =>
“Distributeur”, :foreign_key => “IDDistributeur”
end
create_table :Expay_ZonesGraphiques, :id => false, :primary_key
=> ‘IDZoneGeographique’ do |t|
t.column :IDZoneGeographique, :integer
t.column :CodeZone, :string
t.column :Libelle, :string
t.column :CodePays, :string
end
Mes relations entre les bases ne sont pas pris en compte.
J’ai crée un pays et une zone
$ ruby script/console
Loading development environment.
france = Pay.new
=> #<Pay:0x25864b8 @new_record=true, @attributes={“NomZone”=>nil,
“EstActif”=>nil, “LibelleFr”=>nil}>
france.id = “FR”
=> “FR”
france.EstActif = 1
=> 1
france.NomZone = “EUROPE”
=> “EUROPE”
france.LibelleFr =“FRANCE”
=> “FRANCE”
france.save!
=> true
zone = ZonesGraphique.new
=> #<ZonesGraphique:0x22d31d0 @new_record=true, @attributes=
{“Libelle”=>nil, “CodePays”=>nil, “CodeZone”=>nil}>
zone.id =0
=> 0
zone.Libelle = “AIN”
=> “AIN”
zone.CodePays = “FR”
=> “FR”
zone.CodeZone = “01”
=> “01”
zone.save!
=> true
Ici j’aimeras affiché toute les zones de la france
france.zones.find :all
=> []
Mon tableau est vide .?! Pourtant cette zone existe en bien et le
codePays est bien FR
ZonesGraphique.find :all
=> [#<ZonesGraphique:0x227bc14 @attributes={“Libelle”=>“AIN”,
“CodePays”=>“FR”, “CodeZone”=>“01”, “IDZoneGeographique”=>“0”}>]
Je comprends pas trop … Un petit coup de main
Merci
Bolo M.
[email protected]
http://blog.developpez.com/index.php?blog=30