mercredi 16 septembre 2015

has_many through association | NameError: uninitialized constant Organization::Organizationsuser

I have two models joined with a has_many through association:

Here are the tables:

  create_table "organizations", force: :cascade do |t|
    t.string   "name"
    t.string   "description"
    t.string   "mission"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "organizations_users", force: :cascade do |t|
    t.integer  "user_id"
    t.integer  "organization_id"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "first_name"
    t.string   "last_name"
  end

Here are the models:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :organizationsusers
  has_many :organizations, :through => :organizationsusers

end

class Organization < ActiveRecord::Base
  has_many :organizationsusers
  has_many :users, :through => :organizationsusers
  has_many :categories, as: :categorizable

end

class OrganizationsUser < ActiveRecord::Base
  belongs_to :user 
  belongs_to :organization
end

When I create a new organization via my form it creates adequately. However I keep getting:

NameError: uninitialized constant Organization::Organizationsuser

If I do something like this for example:

o = Organization.last

  Organization Load (0.4ms)  SELECT  "organizations".* FROM "organizations"  ORDER BY "organizations"."id" DESC LIMIT 1
 => #<Organization id: 1, name: "HOU", description: "fkjndskj", mission: "fnskjdfs", created_at: "2015-09-16 07:35:42", updated_at: "2015-09-16 07:35:42">

o.users
NameError: uninitialized constant Organization::Organizationsuser

Why is this happening? Am I misunderstanding something here?

Aucun commentaire:

Enregistrer un commentaire