[英]has_many through association | NameError: uninitialized constant Organization::Organizationsuser
I have two models joined with a has_many through association: 我有两个通过关联与has_many关联的模型:
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? 我在这里误会什么吗?
NameError: uninitialized constant Organization::Organizationsuser NameError:未初始化的常量Organization :: Organizationsuser
Changing organizationsusers
to organizations_users
in your code should fix the error. 更改organizationsusers
到organizations_users
在你的代码应该修正这个错误。
#user.rb
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 :organizations_users
has_many :organizations, :through => :organizations_users
end
#organization.rb
class Organization < ActiveRecord::Base
has_many :organizations_users
has_many :users, :through => :organizations_users
has_many :categories, as: :categorizable
end
I have read you code. 我已经读过您的代码。 You have typo to write your associations. 您有拼写错误来写您的关联。 Please change :organizationsusers
into :organizations_users
. 请将:organizationsusers
更改为:organizations_users
。 I hope this help you 希望对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.