我已经在这个问题上胡思乱想了4天了,我想知道我是否面临一个ActiveRecord错误?我正在尝试将一个用户模型链接到一个标注模型。
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"
endcallout.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]
enduser_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 SQLite3 3::SQLException中有:ActiveRecord::StatementIn有效值:没有这样的列: callouts.user_id:选择1作为“标注”中的一列(“calloutable_id”为NULL,“calloutable_id”为NULL,“user_id”为NULL,“conversation_id”为NULL,“calloutable_type”为NULL)限制为1。
因此,就好像ActiverRecords在我的标注表上查找" user_id“列,而user_id只在连接表的一侧.我对我的模特做了什么错事?为什么我的has_many槽协会没有恢复?
下面是生成的SQL代码:
用户存在(0.2ms)从“用户”(较低(“用户”.“名称”)=较低(‘name10’)和“用户”.“id”= 10)限制1中选择1作为一个
“calloutable_id”为空,“user_id”为空,“conversation_id”=1和“calloutable_type”为空)限制1
SQLite3 3::SQLException:没有这样的列: callouts.user_id:选择1作为“标注”中的一列(“calloutable_id”为空,“calloutable_id”为空,“user_id”为空,“user_id”为NULL,“conversation_id”=1,“calloutable_type”为空)限制1
(0.0ms)回滚事务
在50 in内完成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的错误?我正在尝试将一个用户模型链接到一个标注模型。
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"
endcallout.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]
enduser_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 SQLite3 3::SQLException中有:ActiveRecord::StatementIn有效值:没有这样的列: callouts.user_id:选择1作为“标注”中的一列(“calloutable_id”为NULL,“calloutable_id”为NULL,“user_id”为NULL,“conversation_id”为NULL,“calloutable_type”为NULL)限制为1。
因此,就好像ActiverRecords在我的标注表上查找" user_id“列,而user_id只在连接表的一侧.我对我的模特做了什么错事?为什么我的has_many槽协会没有恢复?
下面是生成的SQL代码:
用户存在(0.2ms)从“用户”(较低(“用户”.“名称”)=较低(‘name10’)和“用户”.“id”= 10)限制1中选择1作为一个
“calloutable_id”为空,“user_id”为空,“conversation_id”=1和“calloutable_type”为空)限制1
SQLite3 3::SQLException:没有这样的列: callouts.user_id:选择1作为“标注”中的一列(“calloutable_id”为空,“calloutable_id”为空,“user_id”为空,“user_id”为NULL,“conversation_id”=1,“calloutable_type”为空)限制1
(0.0ms)回滚事务
在50 in内完成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.)不同的UserS可以参与不同的标注。因此,用户<=>标注是一个"has_many /直通“关系。(不是HABTM,因为我需要自定义连接模型)。所以我可以写@callout.users
2.)一个Calloutable的标注目标(Calloutable要么是用户,要么是PotentialUser)。但是一个可呼叫者可能会被不同的呼叫对象所攻击。因此,它是一个belong_to / has_many多态关系。我还可以写@user.callouts...and和@callout.users..。
但是:情况1或2)中的@callout.users并不意味着相同的事情。
以下是去尾模型:
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之间的关系.
发布于 2015-07-21 12:47:19
修好了!!问题其实是我写的:
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"`我工作得很好!谢谢你对我的耐心和理解。:-)
发布于 2015-07-20 00:13:24
我觉得你让这件事变得更难了。别跟框架作对,它是来帮忙的!
首先,对于join表,使用标准Rails命名约定:按字母顺序排列模型。如果回滚一点,而不是为一个操作创建三个迁移,为什么不:
rails g migration callouts_users callout_id:integer user_id:integer
再加上你的模型中的has_and_belongs_to_many关系,你应该会更好。
https://stackoverflow.com/questions/31505430
复制相似问题