【问题标题】:has_many through association | NameError: uninitialized constant Organization::Organizationsuserhas_many 通过关联 | NameError: 未初始化的常量 Organization::Organizationsuser
【发布时间】: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


    【解决方案1】:

    NameError: 未初始化常量 Organization::Organizationsuser

    在您的代码中将 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
    

    【讨论】:

    • 谢谢!这让o.users 不会抛出错误。但是出于某种原因,对于我创建的任何组织,查询集都是空的。我是否必须将 user_id 作为参数传递?我什至允许使用 def organization_params params.require(:organization).permit! end 的所有强参数,但没有运气
    • @james 你能把你给o.users时生成的查询贴出来吗?
    • o.users User Load (0.3ms) SELECT "users".* FROM "users" INNER JOIN "organizations_users" ON "users"."id" = "organizations_users"."user_id" WHERE "organizations_users"."organization_id" = $1 [["organization_id", 5]] =&gt; #&lt;ActiveRecord::Associations::CollectionProxy []&gt;
    【解决方案2】:

    我已经阅读了你的代码。你有错字来写你的联想。 请将:organizationsusers 更改为:organizations_users。希望对你有帮助

    【讨论】:

    • 谢谢!这让o.users 不会抛出错误。但是出于某种原因,对于我创建的任何组织,查询集都是空的。我是否必须将 user_id 作为参数传递?我什至允许使用 def organization_params params.require(:organization).permit! end 的所有强参数,但没有运气
    猜你喜欢
    • 1970-01-01
    • 2011-10-25
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多