2014-01-08 41 views
1

我試圖讓我的關係解決,但我無法使用這些關聯。無法在rails has_many中查詢:通過關係

所以我有三種型號User,ShopAccess。一個用戶應該有很多商店,一個商店應該有很多用戶。

的車型有:

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 

有沒有人有,爲什麼我收到此錯誤的任何想法?

+0

你百分之百確定你有'類用戶<的ActiveRecord :: Base的 的has_many:訪問 的has_many:商店,通過:訪問 end'而不是'類用戶<的ActiveRecord :: Base的 的has_many:accesss has_many:商店,通過::訪問 結束? – kddeisz

+0

是的,我完全確定。 – Peeyush

回答

1

我在黑暗中那種在這裏取景,但嘗試:

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

這實際上工作!你能告訴我什麼是錯的,這個代碼在做什麼? – Peeyush

+1

「Inflector」將單詞轉換爲複數,將類名轉換爲表名,將模塊化類名轉換爲沒有的類,並將類名轉換爲外鍵。對於複數形式,單數形式和不可數單詞的默認拐點保存在'inflections.rb'中。 http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html(請參閱「ActiveSupport :: Inflector :: Inflections」幫助) – wacko

0

我複製一個新項目的所有代碼。

=> #<User id: 1, email: "[email protected]", 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: "[email protected]", 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 []> 

一切工作正常。 Rails 4.0.1,ruby 2.0.0-p353。 似乎問題在於其他一些代碼。或者在一個錯字,但我已經直接從這裏複製代碼到一個新的模型。

也只是一個盲注:你將每個模型存儲在一個單獨的文件?