Problem with has_many and belongs_to: NameError: uninitialized constant User::Recipy

Hello,

I’m learning to use ORM system. I read a lot of content but i can’t get
it
to work.

class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.column :email, :string
t.column :nome, :string
t.timestamps
end
end

def self.down
drop_table :users
end
end

class CreateRecipies < ActiveRecord::Migration
def self.up
create_table :recipies do |t|
t.column :titulo, :string
t.column :descricao, :string
t.column :user_id, :integer
t.column :recipie_id, :integer
t.timestamps
end
end

def self.down
drop_table :recipies
end
end

class User < ActiveRecord::Base
has_many :recipies
end

class Recipie < ActiveRecord::Base
belongs_to :user
end

At console i’m typing

usr = Users.find(1)

recipie = usr.recipies
NameError: uninitialized constant User::Recipy
from
/Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:492:in
const_missing' from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1909:incompute_type’
from
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/reflection.rb:129:in
send' from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/reflection.rb:129:inklass’
from
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/reflection.rb:137:in
quoted_table_name' from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations/has_many_association.rb:84:inconstruct_sql’
from
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations/association_collection.rb:8:in
initialize' from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations.rb:1128:innew’
from
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations.rb:1128:in
`recipies’
from (irb):2

Why?

Thanks for your attention!


Regards,

Luiz Vitor Martinez C.
cel.: (11) 8187-8662
blog: rubz.org
engineer student at maua.br

NameError: uninitialized constant User::Recipy

Check your spelling.

-e

Luiz Vitor Martinez C. wrote:

class User < ActiveRecord::Base
has_many :recipies
end

class Recipie < ActiveRecord::Base
belongs_to :user
end

At console i’m typing

usr = Users.find(1)

recipie = usr.recipies
NameError: uninitialized constant User::Recipy
from

In Rails the convention is to make Model name singular and the
corresponding class name is plural as in your case it is users so
according to rule the model name should be just User with U capital now
to access data from users table you need to do usr=User.find(1) so will
get your result confirm the singular model name by looking the class
name of the corresponding model in models folder…

try this…

class User < ActiveRecord::Base

has_many :recipies, :class_name => ‘Recipie’
end

class Recipie < ActiveRecord::Base
belongs_to :user
end