2015-11-14 45 views
0

我有以下設置:Rails的關聯:SQLite3的::的SQLException:沒有這樣的列

class Invite < ActiveRecord::Base 
    belongs_to :attending_guest, :class_name => "User", :foreign_key => :attending_guest_id 
    belongs_to :attending_event, :class_name => "Event" 
end 

class Event < ActiveRecord::Base 
    belongs_to :creator, :class_name => "User" 
    has_many :invites 
    has_many :attendees, :through => :invites, :source => :attending_event 
end 

class User < ActiveRecord::Base 
    has_many :created_events, :foreign_key => "creator_id", :class_name => "Event" 
    has_many :invites 
    has_many :attended_events, through: :invites, source: :attending_guest 
end 

以下模式:

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

    create_table "events", force: :cascade do |t| 
    t.string "title" 
    t.string "date" 
    t.text  "description" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.integer "creator_id" 
    t.string "location" 
    end 

    add_index "events", ["creator_id"], name: "index_events_on_creator_id" 

    create_table "invites", force: :cascade do |t| 
    t.datetime "created_at",   null: false 
    t.datetime "updated_at",   null: false 
    t.integer "attending_guest_id" 
    t.integer "attending_event_id" 
    end 

    add_index "invites", ["attending_event_id"], name: "index_invites_on_attending_event_id" 
    add_index "invites", ["attending_guest_id"], name: "index_invites_on_attending_guest_id" 

    create_table "users", force: :cascade do |t| 
    t.string "name" 
    t.string "email" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

end 

建立這種關係,在cnosole後,我得到以下當我運行時出錯:

2.2.0 :001 > u = User.first 
    User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1 
=> #<User id: 1, name: "User One", email: "[email protected]", created_at: "2015-11-14 09:20:26", updated_at: "2015-11-14 09:20:26"> 
2.2.0 :002 > u.attended_events 
    User Load (0.4ms) SELECT "users".* FROM "users" INNER JOIN "invites" ON "users"."id" = "invites"."attending_guest_id" WHERE "invites"."user_id" = ? [[nil, 1]] 
SQLite3::SQLException: no such column: invites.user_id: SELECT "users".* FROM "users" INNER JOIN "invites" ON "users"."id" = "invites"."attending_guest_id" WHERE "invites"."user_id" = ? 
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: invites.user_id: SELECT "users".* FROM "users" INNER JOIN "invites" ON "users"."id" = "invites"."attending_guest_id" WHERE "invites"."user_id" = ? 

任何人都可以解釋發生了什麼?我已經嘗試了很多不同的東西,但只是不明白問題是什麼......任何幫助將非常感謝!謝謝一堆!

+0

爲什麼使用自定義的外鍵的名稱和不遵循Rails的慣例? –

+0

對不起,主要原因是因爲我想了解Active Record中所有可用選項的不同用途。 – hackrnaut

+0

這是一個很好的理由,你應該在你的問題中注意到這一點。 –

回答

1

此錯誤表明您的invites中沒有user_id列。

按照慣例,Rails假定用於在其他模型上保存外鍵的列是該模型的名稱,後綴爲_id。的:foreign_key選項可以讓你設置的外鍵的名稱直接

所以您可以添加:

class User < ActiveRecord::Base 
    has_many :invites, :foreign_key => :attending_guest_id 
end 
+0

是的,這個工作,你的解釋幫助我看到了巨大的事情,非常感謝你! – hackrnaut

相關問題