【问题标题】:Unable to query in a rails has_many :through relation无法在 rails has_many 中查询:通过关系
【发布时间】:2014-01-08 16:35:36
【问题描述】:

我正在努力解决我的关系,但我无法使用这些关联。

所以我有三个模型User、Shop 和Access。一个用户应该有很多商店,一个商店应该有很多用户。

模型是:

class User < ActiveRecord::Base
  has_many :accesses
  has_many :stores, through: :accesses
end

class Store < ActiveRecord::Base
  has_many :accesses
  has_many :users, through: :accesses
end

class Access < ActiveRecord::Base
  belongs_to :user
  belongs_to :store
end

对应的架构是

ActiveRecord::Schema.define(version: 20140108102103) do

  create_table "accesses", force: true do |t|
    t.integer  "user_id"
    t.integer  "store_id"
    t.string   "permission"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "stores", force: true do |t|
    t.string   "tin"
    t.text     "address"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "store_name"
    t.float    "credit"
  end

  create_table "users", force: true 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"
    t.datetime "updated_at"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

现在,当我查询User.first.stores

ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :accesss in model User

有人知道我为什么会收到这个错误吗?

【问题讨论】:

  • 您是否 100% 确定您拥有 class User &lt; ActiveRecord::Base has_many :accesses has_many :stores, through: :accesses end 而不是 class User &lt; ActiveRecord::Base has_many :accesss has_many :stores, through: :accesses end?
  • 是的,我完全确定。

标签: ruby-on-rails ruby-on-rails-4 has-many-through


【解决方案1】:

我在这里有点摸不着头脑,但试试吧:

# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'access', 'accesses'
end

【讨论】:

  • 这确实有效!你能告诉我出了什么问题以及这段代码在做什么吗?
  • Inflector 将单词从单数转换为复数,将类名转换为表名,将模块化类名转换为无类名,将类名转换为外键。复数、单数和不可数词的默认变形保留在inflections.rb 中。 api.rubyonrails.org/classes/ActiveSupport/Inflector.html(也请参阅ActiveSupport::Inflector::Inflections 的帮助)
【解决方案2】:

我已将您的所有代码复制到一个新项目中。

 => #<User id: 1, email: "test@test.com", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2014-01-08 16:52:01", updated_at: "2014-01-08 16:52:01"> 
 2.0.0-p353 :004 > User.first
 User Load (0.2ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
 => #<User id: 1, email: "test@test.com", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2014-01-08 16:52:01", updated_at: "2014-01-08 16:52:01"> 
 2.0.0-p353 :005 > User.first.stores
 User Load (0.2ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
 Store Load (0.2ms)  SELECT "stores".* FROM "stores" INNER JOIN "accesses" ON "stores"."id" = "accesses"."store_id" WHERE "accesses"."user_id" = ?  [["user_id", 1]]
 => #<ActiveRecord::Associations::CollectionProxy []>

一切正常。导轨 4.0.1,红宝石 2.0.0-p353。 似乎问题出在其他代码中。或者是一个错字,但我已将代码直接从这里复制到一个新模型中。

也只是一个盲点:您是否将每个模型存储在单独的文件中?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    相关资源
    最近更新 更多