NameError: uninitialized constant Company::Addres

Od południa walczę z głupim błędem i nie potrafię sobie poradzić :frowning:
Mam zdefiniowane 3 modele:

[code]class Company < ActiveRecord::Base
has_many :address
has_many :person
end

class Address < ActiveRecord::Base
belongs_to :company
end

class Person < ActiveRecord::Base
belongs_to :company
end[/code]
Migracje wyglądają następująco:

[code]class CreateCompanies < ActiveRecord::Migration
def self.up
create_table :companies do |t|
t.string :name # nazwa firmy
t.string :alias # krotka nazwa firmy
t.string :nip # nip
t.string :url # adres strony internetowej
t.string :url_logo # adres do logo firmy (w gif lub jpg)
t.boolean :enabled # czy firma jest aktywna
t.timestamps
end
end

def self.down
drop_table :companies
end
end[/code]

[code]class CreateAddresses < ActiveRecord::Migration
def self.up
create_table :addresses do |t|
t.integer :company_id
t.string :street
t.string :number # numer domu / numer mieszkania
t.string :postcode # kod pocztowy
t.string :city # miasto
t.string :post # jesli poczta znajduje sie w innym miescie
t.string :type # typ adresu (faktura, dostawa)
t.text :description
t.timestamps
end

execute "alter table addresses
          add constraint fk_address_companies
          foreign key(company_id) references companies(id)"

end

def self.down
drop_table :addresses
end
end[/code]

[code]class CreatePeople < ActiveRecord::Migration
def self.up
create_table :people do |t|
t.integer :company_id # firma, w ktorej pracuje osoba
t.string :first_name
t.string :last_name
t.string :phone # telefon stacjonarny
t.string :mobile # telefon komorkowy
t.string :email
t.string :login
t.string :password
t.string :fax
t.string :enabled # czy konto jest aktywne
t.timestamps
end

execute "alter table people 
          add constraint fk_person_companies
          foreign key(company_id) references companies(id)"

end

def self.down
drop_table :people
end
end[/code]
W script/console gdy robię:

[code]c = Company.find(:first) # znajduje pierwszy rekord [OK]
c.person # wyswietla dane o osobie
c.address # wywala error

NameError: uninitialized constant Company::Addres
from /Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:478:in const_missing' from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:1750:incompute_type’
from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/reflection.rb:125:in send' from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/reflection.rb:125:inklass’
from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/has_many_association.rb:152:in construct_sql' from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/has_many_association.rb:6:ininitialize’
from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations.rb:1032:in new' from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations.rb:1032:inaddress’
from (irb):3[/code]
Może Wy coś zauważycie co robię źle…

Co ciekawy, gdy zdefiniuje model następująco:

class Company < ActiveRecord::Base has_many :addresss has_many :person end
c = Company.find(:first)
c.addresss

działa relacja Company::Address

Może “has_many :addresses” ?

Pamiętaj że przy has_many podajesz liczbę mnogą (dlatego powinieneś mieć także has_many :persons).

Rzeczywiście has_many powinno być definiowane w liczbie mnogiej. Eh chyba dziś powinienem sobie zrobić przerwę w pisaniu :stuck_out_tongue:

Dzięki za pomoc :slight_smile:

[quote=radarek]Może “has_many :addresses” ?

Pamiętaj że przy has_many podajesz liczbę mnogą (dlatego powinieneś mieć także has_many :persons).[/quote]
Raczej has_many :people :slight_smile:

[code]>> “person”.pluralize
=> “people”

“people”.singularize
=> “person”[/code]
Faktycznie, aczkolwiek forma ‘persons’ w niektórych przypadkach także jest poprawna jeśli chodzi o angielskie.