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

时间:2015-07-19 20:06:43

标签: ruby-on-rails ruby-on-rails-4 activerecord sqlite has-many-through

我已经在这个问题上苦苦挣扎4天了,我想知道我是不是面对ActiveRecord错误?我正在尝试将用户模型链接到Callout模型。

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

我马上: CalloutsController #create中的ActiveRecord :: StatementInvalid SQLite3 :: SQLException:没有这样的列:callouts.user_id:SELECT 1 AS一个FROM“callouts”WHERE(“callouts”。“calloutable_id”IS NULL和“callouts”。“user_id”IS NULL和“callouts”。“conversation_id” IS NULL和“callouts”。“calloutable_type”IS NULL)LIMIT 1

因此,如果ActiverRecords在我的callouts表中寻找“user_id”列,而user_id只在连接表侧... 我在模特身上做错了什么?为什么我的has_many-trough协会没有被认识到?

这是生成的SQL代码:

用户存在(0.2ms)SELECT 1 AS一个FROM“users”WHERE(LOWER(“users”。“name”)= LOWER('name10')AND“users”。“id”!= 10)LIMIT 1

标注存在(0.6ms)SELECT 1 AS一个FROM“callouts”WHERE(“callouts”。“calloutable_id”IS NULL和“callouts”。“user_id”IS NULL和“callouts”。“conversation_id”= 1 AND“ callouts“。”calloutable_type“IS NULL”LIMIT 1

SQLite3 :: SQLException:没有这样的列:callouts.user_id:SELECT 1 AS一个FROM“callouts”WHERE(“callouts”。“calloutable_id”IS NULL和“callouts”。“user_id”IS NULL和“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

=============================================== ====&GT; 我已经在这个问题上苦苦挣扎了4天,我想知道我是否面对ActiveRecord错误?我正在尝试将用户模型链接到Callout模型。

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

我马上: CalloutsController #create中的ActiveRecord :: StatementInvalid SQLite3 :: SQLException:没有这样的列:callouts.user_id:SELECT 1 AS一个FROM“callouts”WHERE(“callouts”。“calloutable_id”IS NULL和“callouts”。“user_id”IS NULL和“callouts”。“conversation_id” IS NULL和“callouts”。“calloutable_type”IS NULL)LIMIT 1

因此,如果ActiverRecords在我的callouts表中寻找“user_id”列,而user_id只在连接表侧... 我在模特身上做错了什么?为什么我的has_many-trough协会没有被认识到?

这是生成的SQL代码:

用户存在(0.2ms)SELECT 1 AS一个FROM“users”WHERE(LOWER(“users”。“name”)= LOWER('name10')AND“users”。“id”!= 10)LIMIT 1

标注存在(0.6ms)SELECT 1 AS一个FROM“callouts”WHERE(“callouts”。“calloutable_id”IS NULL和“callouts”。“user_id”IS NULL和“callouts”。“conversation_id”= 1 AND“ callouts“。”calloutable_type“IS NULL”LIMIT 1

SQLite3 :: SQLException:没有这样的列:callouts.user_id:SELECT 1 AS一个FROM“callouts”WHERE(“callouts”。“calloutable_id”IS NULL和“callouts”。“user_id”IS NULL和“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

=============================&GT;修改

确定,

我还有另一条线索。我认为错误在于用户模型,但我不知道如何编写它。

简而言之:

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

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

但是:情况1)中的@ callout.users或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:callouts)我想我只需要改变用户收到的@ user.callouts和用户提出的@ user.callouts之间的轨道...

2 个答案:

答案 0 :(得分:0)

我觉得你好像比现在更难以做到这一点。不要与框架作斗争,它会在这里提供帮助!

首先,对于您的连接表,使用标准的Rails命名约定:按字母顺序排序模型。如果您回滚一点,而不是为一个操作创建三个迁移,为什么不:

rails g migration callouts_users callout_id:integer user_id:integer

在模型中加上has_and_belongs_to_many关系,你应该很高兴。

答案 1 :(得分:-1)

固定!! 问题实际上是我写的:

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"`

我的工作很完美! 感谢您对neewbie的耐心和理解......: - )