Rails 4 - 破坏 HABTM 记录导致 UndefinedTable
Rails 4 - destroying a HABTM record results in UndefinedTable
我在事件和用户之间有 has_and_belongs_to_many 关系。
user.rb
class User < ActiveRecord::Base
rolify
has_and_belongs_to_many :events, join_table: :events_users
after_create :assign_default_role
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
def assign_default_role
add_role(:basic)
end
end
event.rb
class Event < ActiveRecord::Base
rolify
has_and_belongs_to_many :users, join_table: :events_users
end
当我想删除事件时,我会做两件事:
- 删除所有关联(来自 events_users table)
- 销毁事件(来自事件table)
所以我写了这段代码来实现:
events_controller.rb
def destroy
@event.users.clear #remove all associations
@event.destroy #destroy event
respond_to do |format|
flash[:success] = 'Event was successfully deleted.'
format.html { redirect_to events_url }
format.json { head :no_content }
end
end
清除联接 table 中的关联效果很好 (@event.users.clear)。
我在尝试销毁实际事件 (@event.destroy) 时遇到错误。
(0.7ms) BEGIN
SQL (0.4ms) DELETE FROM "events_users" WHERE "events_users"."event_id" = [["event_id", 13]]
PG::UndefinedTable: ERROR: relation "events_roles" does not exist
LINE 5: WHERE a.attrelid = '"events_roles"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"events_roles"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
(0.3ms) ROLLBACK
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "events_roles" does not exist
LINE 5: WHERE a.attrelid = '"events_roles"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"events_roles"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
它似乎在寻找 "events_roles" table,我不明白为什么。
(events_users 有道理,但 events_roles 出乎意料)
感谢您的帮助。
这将与您在活动 class 中的 'rolify' 有关。
class Event < ActiveRecord::Base
rolify
...
这是 Rolify gem,它可能会在数据库中生成额外的 tables/fields。
我在事件和用户之间有 has_and_belongs_to_many 关系。
user.rb
class User < ActiveRecord::Base
rolify
has_and_belongs_to_many :events, join_table: :events_users
after_create :assign_default_role
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
def assign_default_role
add_role(:basic)
end
end
event.rb
class Event < ActiveRecord::Base
rolify
has_and_belongs_to_many :users, join_table: :events_users
end
当我想删除事件时,我会做两件事:
- 删除所有关联(来自 events_users table)
- 销毁事件(来自事件table)
所以我写了这段代码来实现:
events_controller.rb
def destroy
@event.users.clear #remove all associations
@event.destroy #destroy event
respond_to do |format|
flash[:success] = 'Event was successfully deleted.'
format.html { redirect_to events_url }
format.json { head :no_content }
end
end
清除联接 table 中的关联效果很好 (@event.users.clear)。
我在尝试销毁实际事件 (@event.destroy) 时遇到错误。
(0.7ms) BEGIN
SQL (0.4ms) DELETE FROM "events_users" WHERE "events_users"."event_id" = [["event_id", 13]]
PG::UndefinedTable: ERROR: relation "events_roles" does not exist
LINE 5: WHERE a.attrelid = '"events_roles"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"events_roles"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
(0.3ms) ROLLBACK
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "events_roles" does not exist
LINE 5: WHERE a.attrelid = '"events_roles"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"events_roles"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
它似乎在寻找 "events_roles" table,我不明白为什么。
(events_users 有道理,但 events_roles 出乎意料)
感谢您的帮助。
这将与您在活动 class 中的 'rolify' 有关。
class Event < ActiveRecord::Base
rolify
...
这是 Rolify gem,它可能会在数据库中生成额外的 tables/fields。