[英]Rails Associations: SQLite3::SQLException: no such column
I have the following setup: 我有以下设置:
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
The following schema: 以下架构:
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
After creating this relationship, in cnosole, I get the following error when I run: 创建此关系后,在鞋垫中,运行时出现以下错误:
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: "user@example.com", 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" = ?
Can anyone explain what's going on? 谁能解释这是怎么回事? I've tried many different things but just cannot understand what the problem is... Any help would be much appreciated! 我已经尝试了许多不同的方法,但是无法理解问题出在什么地方。任何帮助将不胜感激! Thanks a bunch! 谢谢一群!
This error comes to show that you don't have an user_id
column in the invites
. 该错误表明您的invites
没有user_id
列。
By convention, Rails assumes that the column used to hold the foreign key on the other model is the name of this model with the suffix _id added. 按照惯例,Rails假定用于保留其他模型上的外键的列是该模型的名称,并添加了后缀_id。 The :foreign_key option lets you set the name of the foreign key directly :foreign_key选项可让您直接设置外键的名称
So you may add: 因此,您可以添加:
class User < ActiveRecord::Base
has_many :invites, :foreign_key => :attending_guest_id
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.