Rails has_many :through 관계에서 쿼리할 수 없습니다.
-
20-12-2019 - |
문제
관계를 정리하려고 노력하고 있지만 연결을 사용하는 데 문제가 있습니다.
그래서 세 가지 모델이 있어요. 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
왜 이 오류가 발생하는지 아는 사람이 있나요?
해결책
나는 여기 어둠 속에서 촬영하고 있지만 시도해보십시오.
# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'access', 'accesses'
end
다른 팁
새 프로젝트에 모든 코드를 복사했습니다.
=> #<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.다른 코드에 문제가 있는 것 같습니다.또는 오타가 있지만 여기에서 새 모델로 코드를 직접 복사했습니다.
또한 블라인드 샷입니다.각 모델을 별도의 파일에 저장합니까?
제휴하지 않습니다 StackOverflow