Const_missing error of has_many through association

Hi,

I am new to ruby on rails. I am building an association join model
and encountered the following error. Does anyone know what am I
missing?

NameError in StartController#home

d:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:100:in `const_missing’: uninitialized constant Membership

I have the following tables relationships

Users -------< Memberships >------- Teams

I have three model classes defined as below

class Users < ActiveRecord::Base
has_many :memberships
has_many :teams, :through => :memberships
end

class Teams < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :memberships
end

class Memberships < ActiveRecord::Base
belongs_to :team
belongs_to :user
end

My ApplicationController has the following code. My other
controllers are empty.

class ApplicationController < ActionController::Base
def home
@user = Users.find(@params[“id”])# Get user based on id
@memteam = @user.teams # Get all teams that this user has joined
end
end

When I load my page, I got those errors

NameError in StartController#home
d:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:100:in `const_missing’: uninitialized constant Membership

Does anyone know what happen?
Thank you.

Arnold.

Arnold Ng wrote:

class Memberships < ActiveRecord::Base
belongs_to :team
belongs_to :user
end

When I load my page, I got those errors

NameError in StartController#home
d:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:100:in `const_missing’: uninitialized constant Membership

Does anyone know what happen?

Your model class name “Memberships” is plural, while the error is that
Rails can’t find the class “Membership”. Rails convention is for the
model class to be singular and the database table to be plural.

–
Josh S.
http://blog.hasmanythrough.com

Thanks Josh, it works.

Arnold.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs