Reputation: 301
i have project with ruby on rails 3.2.14. i have 2 table named "countries" and "users". in "users" table have FK named "countries_id".
on model i have make table relation like:
in countries.rb
i have set relationship like:
class Countries < ActiveRecord::Base
self.primary_key = "id"
has_many :users, :dependent => :destroy # plural
end
and in users.rb like:
class Users < ActiveRecord::Base
self.primary_key = "id"
belongs_to :countries,
:class_name => "Countries",
:foreign_key => "countries_id"
end
and when i want to check plural relationship, it's didn't.. i type in rails console command like below
c=Countries.find_by_id(1)
and then when i type command u=c.users
, i have some error
the error:
c=Countries.find_by_id(1)
Countries Load (0.3ms) SELECT `countries`.* FROM `countries` WHERE `countries`.`id` = 1 LIMIT 1
=> #<Countries id: 1, code: "IND", name: "Indonesia", created_at: "2014-02-17 09:18:11", updated_at: "2014-02-17 09:18:11">
1.9.3-p484 :012 > u=c.users
NameError: uninitialized constant Countries::User
from /home/vinra/.rvm/gems/[email protected]/gems/activerecord-3.2.14/lib/active_record/inheritance.rb:111:in `compute_type'
from /home/vinra/.rvm/gems/[email protected]/gems/activerecord-3.2.14/lib/active_record/reflection.rb:172:in `klass'
from /home/vinra/.rvm/gems/[email protected]/gems/activerecord-3.2.14/lib/active_record/associations/association.rb:118:in `klass'
from /home/vinra/.rvm/gems/[email protected]/gems/activerecord-3.2.14/lib/active_record/associations/association.rb:171:in `find_target?'
from /home/vinra/.rvm/gems/[email protected]/gems/activerecord-3.2.14/lib/active_record/associations/collection_association.rb:334:in `load_target'
from /home/vinra/.rvm/gems/[email protected]/gems/activerecord-3.2.14/lib/active_record/associations/collection_proxy.rb:44:in `load_target'
from /home/vinra/.rvm/gems/[email protected]/gems/activerecord-3.2.14/lib/active_record/associations/collection_proxy.rb:87:in `method_missing'
from /home/vinra/.rvm/gems/[email protected]/gems/railties-3.2.14/lib/rails/commands/console.rb:47:in `start'
from /home/vinra/.rvm/gems/[email protected]/gems/railties-3.2.14/lib/rails/commands/console.rb:8:in `start'
from /home/vinra/.rvm/gems/[email protected]/gems/railties-3.2.14/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
1.9.3-p484 :013 > ^C
1.9.3-p484 :013 >
i really hopping somebody knowing my problem, anyway thank you,, sorry for my bad english :)
Upvotes: 1
Views: 2250
Reputation: 33542
The Problem is that Rails Active Record
Models cant be Plural. You created the models with plural,so change them to singular or better way is to create them again with singular names.
Countries
should be Country
and Users
should be User
.
For more information on Naming Conventions,see This.
Hope it helps!
Upvotes: 0
Reputation: 3079
You should change a lot of things in your code:
app/models/country.rb
class Country < ActiveRecord::Base
has_many :users, :dependent => :destroy
end
app/models/user.rb
class Users < ActiveRecord::Base
belongs_to :country,
:foreign_key => "countries_id"
end
And use it like this:
country = Country.find(1)
p country.users
Upvotes: 3