Impossibile interrogare in una rotaia has_many: attraverso la relazione
-
20-12-2019 - |
Domanda
Sto cercando di far funzionare le mie relazioni, ma sto avendo problemi a usare le associazioni.
Quindi ho tre modelli User
, Shop
e Access
.Un utente dovrebbe avere molti negozi e un negozio dovrebbe avere molti utenti.
I modelli sono:
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
.
Lo schema corrispondente è
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
.
Ora, quando query generanodicetagcode
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :accesss in model User
.
Qualcuno ha qualche idea del motivo per cui ricevo questo errore?
Soluzione
Sono una specie di sparatoria nel buio qui, ma prova:
# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'access', 'accesses'
end
. Altri suggerimenti
Ho copiato tutto il tuo codice su un nuovo progetto.
=> #<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 []>
.
Tutto funziona bene.Rails 4.0.1, Ruby 2.0.0-P353.
Sembra che il problema sia in qualche altro codice.O in un errore di battitura, ma ho copiato il codice direttamente da qui in un nuovo modello.
anche un colpo cieco: immagazzini ogni modello in un file separato?