我試圖讓我的關係解決,但我無法使用這些關聯。無法在rails has_many中查詢:通過關係
所以我有三種型號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
有沒有人有,爲什麼我收到此錯誤的任何想法?
你百分之百確定你有'類用戶<的ActiveRecord :: Base的 的has_many:訪問 的has_many:商店,通過:訪問 end'而不是'類用戶<的ActiveRecord :: Base的 的has_many:accesss has_many:商店,通過::訪問 結束? – kddeisz
是的,我完全確定。 – Peeyush