Rails 4 - Has_many 到 - StatementInvalid - SQLite3::SQLException:没有这样的列:

Rails 4 - Has_many through - StatementInvalid - SQLite3::SQLException: no such column:

我已经在这个问题上苦苦挣扎了 4 天,我想知道我是否没有遇到 ActiveRecord 错误?我正在尝试 link 将用户模型转换为标注模型。

user.rb

class User < ActiveRecord::Base
    has_many :callouts_users
    has_many :callouts, through: :callouts_users    
    devise  :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable     
    has_many :posts, inverse_of: :creator
    has_many :callouts, as: :calloutable    
    has_many :profiles, as: :profileable, :validate => true     
    validates :name, presence: true
    validates_uniqueness_of :name, :case_sensitive => false, :message => "This name has already been taken"
end

callout.rb

class Callout < ActiveRecord::Base
    has_many :callouts_users
    has_many :users, through: :callouts_users
    belongs_to :conversation
    belongs_to :calloutable, polymorphic: true, class_name: "::Callout", :validate => true  
    validates :conversation, presence: true
    validates :calloutable, presence: true
    validates_uniqueness_of :calloutable_id, :scope => [:user_id, :conversation_id, :calloutable_type]
end

user_callout.rb

class UserCallout < ActiveRecord::Base
    belongs_to :user
    belongs_to :callout
    validates :type, presence: true, 
    validates :type, :inclusion=> { :in => ["up", "down"] }
end

我的迁移如下:

..._create_callouts_users.rb

class CreateCalloutsUsers < ActiveRecord::Migration
  def change
    create_table :callouts_users do |t|
      t.timestamps null: false
    end
  end
end

..._add_callout_to_callouts_users.rb

class AddCalloutToCalloutsUsers < ActiveRecord::Migration
  def change
    add_reference :callouts_users, :callout, index: true
    add_foreign_key :callouts_users, :callouts
  end
end

..._add_user_to_callouts_users.rb

class AddUserToCalloutsUsers < ActiveRecord::Migration
  def change
    add_reference :callouts_users, :user, index: true
    add_foreign_key :callouts_users, :users
  end
end

当我尝试做类似

的事情时
@callout = @conversation.callouts.find_by(calloutable: @user) 
if(@callout.nil?) @callout = Callout.new(conversation: @conversation, calloutable: @user)
@callout.users << current_user
@callout.save

我立即拥有: ActiveRecord::StatementInvalid 在 CalloutsController#create SQLite3::SQLException:没有这样的列:callouts.user_id:SELECT 1 作为一个来自“callouts”的地方(“callouts”。“calloutable_id”是 NULL AND“callouts”。“ =147=]" IS NULL AND "callouts"."conversation_id" IS NULL AND "callouts"."calloutable_type" IS NULL) LIMIT 1

所以好像 ActiverRecords 在我的标注 table 上寻找“user_id”列,而 user_id 仅在连接 table 端...... 我在我的模型上做错了什么?为什么我的 has_many - 槽关联未被识别?

这是生成的 SQL 代码:

用户存在(0.2 毫秒)SELECT 1 作为一个来自“用户”,WHERE (LOWER(“users”.“name”) = LOWER('name10') AND “users”。 id" != 10) 限制 1

标注存在(0.6 毫秒)SELECT 1 作为一个来自“标注”,其中(“标注”。“calloutable_id”为空且“标注”。user_id” IS NULL AND "callouts"."conversation_id" = 1 AND "callouts"."calloutable_type" IS NULL) LIMIT 1

SQLite3::SQLException:没有这样的列:callouts.user_id:SELECT 1 作为一个来自“callouts”的地方(“callouts”。“calloutable_id”为空且“ callouts"."user_id" IS NULL AND "callouts"."conversation_id" = 1 AND "callouts"."calloutable_type" IS NULL) LIMIT 1

(0.0ms) 回滚事务

在 50 毫秒内完成 500 个内部服务器错误

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

  create_table "callouts", force: :cascade do |t|
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.integer  "conversation_id"
    t.integer  "calloutable_id"
    t.string   "calloutable_type"
  end

  add_index "callouts", ["calloutable_type", "calloutable_id"], name: "index_callouts_on_calloutable_type_and_calloutable_id"
  add_index "callouts", ["conversation_id"], name: "index_callouts_on_conversation_id"

  create_table "callouts_users", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
    t.integer  "callout_id"
  end

  add_index "callouts_users", ["callout_id"], name: "index_callouts_users_on_callout_id"
  add_index "callouts_users", ["user_id"], name: "index_callouts_users_on_user_id"

  create_table "conversations", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "posts", force: :cascade do |t|
    t.datetime "created_at",      null: false
   t.datetime "updated_at",      null: false
    t.integer  "conversation_id"
    t.integer  "creator_id"
    t.text     "title"
    t.text     "content"
 end

  add_index "posts", ["conversation_id"], name: "index_posts_on_conversation_id"
  add_index "posts", ["creator_id"], name: "index_posts_on_creator_id"

  create_table "potential_users", force: :cascade do |t|
    t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
  end

  create_table "profiles", force: :cascade do |t|
   t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.integer  "profileable_id"
    t.string   "profileable_type"
    t.string   "description"
 end

  add_index "profiles", ["description"], name: "index_profiles_on_description", unique: true
  add_index "profiles", ["profileable_type", "profileable_id"], name: "index_profiles_on_profileable_type_and_profileable_id"

  create_table "users", force: :cascade do |t|
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    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.string   "name"
  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

============================================= ======> 我已经在这个问题上苦苦挣扎了 4 天,我想知道我是否没有遇到 ActiveRecord 错误?我正在尝试 link 将用户模型转换为标注模型。

user.rb

class User < ActiveRecord::Base
    has_many :callouts_users
    has_many :callouts, through: :callouts_users    
    devise  :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable     
    has_many :posts, inverse_of: :creator
    has_many :callouts, as: :calloutable    
    has_many :profiles, as: :profileable, :validate => true     
    validates :name, presence: true
    validates_uniqueness_of :name, :case_sensitive => false, :message => "This name has already been taken"
end

callout.rb

class Callout < ActiveRecord::Base
    has_many :callouts_users
    has_many :users, through: :callouts_users
    belongs_to :conversation
    belongs_to :calloutable, polymorphic: true, class_name: "::Callout", :validate => true  
    validates :conversation, presence: true
    validates :calloutable, presence: true
    validates_uniqueness_of :calloutable_id, :scope => [:user_id, :conversation_id, :calloutable_type]
end

user_callout.rb

class UserCallout < ActiveRecord::Base
    belongs_to :user
    belongs_to :callout
    validates :type, presence: true, 
    validates :type, :inclusion=> { :in => ["up", "down"] }
end

我的迁移如下:

..._create_callouts_users.rb

class CreateCalloutsUsers < ActiveRecord::Migration
  def change
    create_table :callouts_users do |t|
      t.timestamps null: false
    end
  end
end

..._add_callout_to_callouts_users.rb

class AddCalloutToCalloutsUsers < ActiveRecord::Migration
  def change
    add_reference :callouts_users, :callout, index: true
    add_foreign_key :callouts_users, :callouts
  end
end

..._add_user_to_callouts_users.rb

class AddUserToCalloutsUsers < ActiveRecord::Migration
  def change
    add_reference :callouts_users, :user, index: true
    add_foreign_key :callouts_users, :users
  end
end

当我尝试做类似

的事情时
@callout = @conversation.callouts.find_by(calloutable: @user) 
if(@callout.nil?) @callout = Callout.new(conversation: @conversation, calloutable: @user)
@callout.users << current_user
@callout.save

我立即拥有: ActiveRecord::StatementInvalid 在 CalloutsController#create SQLite3::SQLException:没有这样的列:callouts.user_id:SELECT 1 作为一个来自“callouts”的地方(“callouts”。“calloutable_id”是 NULL AND“callouts”。“ =147=]" IS NULL AND "callouts"."conversation_id" IS NULL AND "callouts"."calloutable_type" IS NULL) LIMIT 1

所以好像 ActiverRecords 在我的标注 table 上寻找“user_id”列,而 user_id 仅在连接 table 端...... 我在我的模型上做错了什么?为什么我的 has_many - 槽关联未被识别?

这是生成的 SQL 代码:

用户存在(0.2 毫秒)SELECT 1 作为一个来自“用户”,WHERE (LOWER(“users”.“name”) = LOWER('name10') AND “users”。 id" != 10) 限制 1

标注存在(0.6 毫秒)SELECT 1 作为一个来自“标注”,其中(“标注”。“calloutable_id”为空且“标注”。user_id” IS NULL AND "callouts"."conversation_id" = 1 AND "callouts"."calloutable_type" IS NULL) LIMIT 1

SQLite3::SQLException:没有这样的列:callouts.user_id:SELECT 1 作为一个来自“callouts”的地方(“callouts”。“calloutable_id”为空且“ callouts"."user_id" IS NULL AND "callouts"."conversation_id" = 1 AND "callouts"."calloutable_type" IS NULL) LIMIT 1

(0.0ms) 回滚事务

在 50 毫秒内完成 500 个内部服务器错误

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

  create_table "callouts", force: :cascade do |t|
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.integer  "conversation_id"
    t.integer  "calloutable_id"
    t.string   "calloutable_type"
  end

  add_index "callouts", ["calloutable_type", "calloutable_id"], name: "index_callouts_on_calloutable_type_and_calloutable_id"
  add_index "callouts", ["conversation_id"], name: "index_callouts_on_conversation_id"

  create_table "callouts_users", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
    t.integer  "callout_id"
  end

  add_index "callouts_users", ["callout_id"], name: "index_callouts_users_on_callout_id"
  add_index "callouts_users", ["user_id"], name: "index_callouts_users_on_user_id"

  create_table "conversations", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "posts", force: :cascade do |t|
    t.datetime "created_at",      null: false
   t.datetime "updated_at",      null: false
    t.integer  "conversation_id"
    t.integer  "creator_id"
    t.text     "title"
    t.text     "content"
 end

  add_index "posts", ["conversation_id"], name: "index_posts_on_conversation_id"
  add_index "posts", ["creator_id"], name: "index_posts_on_creator_id"

  create_table "potential_users", force: :cascade do |t|
    t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
  end

  create_table "profiles", force: :cascade do |t|
   t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.integer  "profileable_id"
    t.string   "profileable_type"
    t.string   "description"
 end

  add_index "profiles", ["description"], name: "index_profiles_on_description", unique: true
  add_index "profiles", ["profileable_type", "profileable_id"], name: "index_profiles_on_profileable_type_and_profileable_id"

  create_table "users", force: :cascade do |t|
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    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.string   "name"
  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

=============================> 编辑

好的,

我还有一条线索。我认为错误出在用户模型上,但我不知道怎么写。

简而言之:

1.) 不同的用户可以参与不同的标注。所以 User <=> Callout 是一个“has_many / through”关系。 (不是 HABTM,因为我需要自定义连接模型)。所以我可以写 @callout.users

2.) 一个 Callout 指向一个 Calloutable(Calloutable 是 User 或 PotentialUser)。但是 Calloutable 可能会被不同的 Callouts 定位。所以它是一个 belong_to / has_many 多态关系。我可以写@user.callouts...还有@callout.users...

但是:@callout.users 在情况 1) 或 2) 中的意思并不相同。

详细型号如下:

class Callout < ActiveRecord::Base
    has_many :callouts_users
    has_many :users, through: :callouts_users

    belongs_to :calloutable, polymorphic: true, class_name: "::Callout", :validate => true    
    validates :calloutable, presence: true
    validates_uniqueness_of :calloutable_id, :scope => [:user_id, :conversation_id, :calloutable_type]

    belongs_to :conversation
    validates :conversation, presence: true
end

class CalloutsUser < ActiveRecord::Base
    belongs_to :user
    belongs_to :callout
    validates_uniqueness_of :user_id, :scope => [:callout_id]
end

class User < ActiveRecord::Base
    has_many :callouts_users
    has_many :callouts, through: :callouts_users
    has_many :callouts, as: :calloutable

    devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
    has_many :posts, inverse_of: :creator    
    has_many :profiles, as: :profileable, :validate => true 

    validates :name, presence: true
    validates_uniqueness_of :name, :case_sensitive => false, :message => "This name has already been taken"
end

class PotentialUser < ActiveRecord::Base
    has_many :callouts, as: :calloutable      
    has_one :profile, as: :profileable, :validate => true
    validates :profile, presence: true
end

有重写用户模型部分的想法吗? (2 has_many:标注)我想我只需要在用户收到的@user.callouts和用户制作的@user.callouts之间对rails进行区分...

我觉得好像你把这件事变得比需要的更难了。别跟框架打架了,它来帮忙!

首先,对于您的加入 table,请使用标准 Rails 命名约定:按字母顺序排列模型。如果你回滚一点,而不是为一个动作创建三个迁移,为什么不呢:

rails g migration callouts_users callout_id:integer user_id:integer

再加上你模型中的 has_and_belongs_to_many 关系,你应该可以开始了。

已修复!! 问题实际上是我写的:

has_many :callouts_users
has_many :callouts, through: :callouts_users
has_many :callouts, as: :calloutable

所以我定义了两次 has_many :callouts,。当然,Rails 不知道如何理解 @callout.users

有:

has_many :callouts_users
has_many :callouts, through: :callouts_users, source: "call", class_name: "Call"
has_many :callins, as: :callable, class_name: "Call"`

我工作得很好! 感谢您对我这个新手的耐心和理解...:-)