【发布时间】:2015-09-16 07:55:16
【问题描述】:
我有两个模型通过关联加入了 has_many:
这是表格:
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
以下是模型:
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
当我通过我的表单创建一个新组织时,它会充分创建。但是我不断得到:
NameError: uninitialized constant Organization::Organizationsuser
例如,如果我这样做:
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
为什么会这样?我是不是误会了什么?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 activerecord associations