Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails PG::UndefinedColumn:错误:column courts.user\u id不存在_Ruby On Rails_Ruby_Postgresql_Activerecord_Rspec - Fatal编程技术网

Ruby on rails PG::UndefinedColumn:错误:column courts.user\u id不存在

Ruby on rails PG::UndefinedColumn:错误:column courts.user\u id不存在,ruby-on-rails,ruby,postgresql,activerecord,rspec,Ruby On Rails,Ruby,Postgresql,Activerecord,Rspec,这是我运行文件测试时收到的错误 当我尝试使用“destroy”方法从数据库中删除用户时,错误出现在第83行 有问题的回购协议是否及时冻结 错误: ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column courts.user_id does not exist LINE 1: SELECT "courts".* FROM "courts" WHERE

这是我运行文件测试时收到的错误

当我尝试使用“destroy”方法从数据库中删除用户时,错误出现在第83行

有问题的回购协议是否及时冻结

错误:

ActiveRecord::StatementInvalid:
       PG::UndefinedColumn: ERROR:  column courts.user_id does not exist
       LINE 1: SELECT "courts".* FROM "courts" WHERE "courts"."user_id" = $...

我试图通过以下方式将桌面用户与桌面球场连接起来:

  • 用户是许多法院的管理员

  • 法院只能有一名管理人

如您所见,“Court”有一个外键链接到“users”,但称为“administrator”

我希望“管理员”是用户的别名,因为将来用户和法院可能会有更多的关系,例如“所有者”

我可以看到active record正在生成此查询:

SELECT "courts".* FROM "courts" WHERE "courts"."user_id" = $...
如果Active Record以这种方式构建查询,也许可以解决这个问题

SELECT "courts".* FROM "courts" WHERE "courts"."administrator_id" = $...
但我不知道怎么做,也不知道这样做是否明智

也许还有别的办法。更整洁的东西,我觉得我做的不够好

你推荐什么

models/user.rb

class User < ActiveRecord::Base
    before_save :format_input
    # extend Devise::Models
    # Include default devise modules        .
    devise  :database_authenticatable,                
            # :validatable,
            # :recoverable,
            # :rememberable,
            # :trackable,             
            # :confirmable,
            # :omniauthable,
            :registerable
    
    # note that this include statement comes AFTER the devise block above
    include DeviseTokenAuth::Concerns::User
    validates :first_name, presence: true, length: { in: 1..20 }
    validates :last_name, presence: true, length: { in: 1..20 }
    validates :email, uniqueness: true
    validates_format_of :email, with: /@/
    validates :password, presence: true, length: { in: 8..20 }, :on => :create

    has_many :courts, dependent: :destroy
    
    private

    def format_input
            self.first_name = first_name.downcase.titleize
            self.last_name = last_name.downcase.titleize
            self.email = email.downcase
    end
end
class用户:create
有很多:法院、附属法院和法院
私有的
def格式输入
self.first\u name=first\u name.downcase.titleize
self.last\u name=last\u name.downcase.titleize
self.email=email.downcase
结束
结束
models/court.rb

class Court < ApplicationRecord 

  belongs_to :administrator, :class_name => :user
  validates :name, presence: true, length: { in: 1..20 }
  validates :address, presence: true, length: { in: 1..50 }
  validates :description, presence: true, length: { in: 1..100 }
  validates :description, presence: true, length: { in: 1..100 }

end
class-Court<申请记录
属于:管理员,:类\u名称=>:用户
验证:name,presence:true,length:{in:1..20}
验证:地址,状态:true,长度:{in:1..50}
验证:description,presence:true,length:{in:1..100}
验证:description,presence:true,length:{in:1..100}
结束

问题在这一行

has_many :courts, dependent: :destroy
默认情况下,ActiveRecord假定外键名为
\u id
。在这种情况下,当您删除一个用户时,ActiveRecord试图使用不存在的
user\u id
外键删除关联的法庭。将
foreign\u键
选项传递给
has\u many
调用

has_many :courts, dependent: :destroy, foreign_key: :administrator_id
从:

:外键

指定用于关联的外键。默认情况下,这被猜测为该类的小写名称,并带有
\u id
后缀。因此,建立
关联的Person类将使用
Person\u id
作为默认的
:外键

还有一个问题。您应该在
Court
模型中的
atto
调用中将大写的类名作为字符串传递

belongs_to :administrator, class_name: 'User'


这与问题无关,但您可能需要检查在删除用户记录时是否可以删除法庭。

请将相关代码(您的型号)添加到问题本身。社区不接受仅仅链接到您的github回购协议或其他外部来源,因为您不应该期望愿意花时间回答您的问题的人不得不四处挖掘,这也会导致链接腐烂,随着时间的推移,这些问题变得毫无价值。谢谢你的反馈。我在问题中加入了相关的代码,以便使这个问题更容易被其他人阅读。谢谢,兄弟。这就是解决办法。@IvanDerlich不客气。