【问题标题】:Using STI with a has_many causes active record to search for subclass table将 STI 与 has_many 一起使用会导致活动记录搜索子类表
【发布时间】:2018-11-07 03:03:22
【问题描述】:

我有一个用户模型和 3 个使用单表继承的子类。所有者、客户和管理员。 (类型列已添加到用户表中)。

#user.rb
class User < ApplicationRecord
    devise :database_authenticatable,:registerable,
         :jwt_authenticatable,
         jwt_revocation_strategy: JWTBlacklist

end

#client.rb
class Client < User
end

#admin.rb
class Admin < User
end

#owner.rb
class Owner < User
  has_many :clubs
end

#club.rb
class Club < ApplicationRecord
  belongs_to :owner
  has_one :address
  has_many :courts
end

Owner 模型与 Club 模型具有 has_many 关系。当我以所有者身份登录我的应用程序,然后尝试添加新俱乐部时。使用

@club= current_user.clubs.create!(club_params)

我收到以下错误。

ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.owners: INSERT INTO "clubs" ("owner_id", "created_at", "updated_at") VALUES (?, ?, ?)):

有没有办法让它搜索用户表?

【问题讨论】:

  • 能否请您添加您的俱乐部型号。我怀疑这个错误在那里。
  • 我添加了俱乐部模型

标签: ruby-on-rails


【解决方案1】:

在这里找到答案Rails STI and has_many through association not working, SQLException: no such table

我不得不使用

spring stop

并重置数据库

rails db:reset

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    相关资源
    最近更新 更多