【问题标题】:Rails 5, Query Interface Is Not Working on new added columnsRails 5,查询接口不适用于新添加的列
【发布时间】:2018-07-19 19:58:54
【问题描述】:

嘿,我遇到了一个非常奇怪的问题。我无法对模型中新添加的列使用任何查询。

事情是这样的,我得到了这个模型;

  create_table "abouts", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "slug"
    t.index ["slug"], name: "index_abouts_on_slug"
  end

并且我已在此迁移中添加了一个名为 sort_index 的整数列并已迁移;

class AddSortIndexToAbout < ActiveRecord::Migration[5.1]
  def change
    add_column :abouts, :sort_index, :integer
  end
end

当我执行About.minimum(:sort_index) 时,它会返回nil

当我尝试About.where('sort_index &gt; ?',0) 它返回一个空关系(我确定所有关于对象的 sort_index 都已设置并且大于 0)。

当我做puts About.all

=> #<ActiveRecord::Relation [#<About id: 1, created_at: "2018-07-19 19:34:56", updated_at: "2018-07-19 19:34:56", sort_index: 1>, #<About id: 2, created_at: "2018-07-19 19:35:04", updated_at: "2018-07-19 19:35:04", sort_index: 2>]>

当我做About.where("id &gt; ?",0).first.sort_index

它返回1(这是它应该返回的)。

该列在表中,它有它的值,但我不能在任何查询接口方法上使用它(我尝试了另一个新创建的整数列,它也是如此)

它在 Rails 控制台上返回相同的结果。

这是我的模型

class About < ApplicationRecord
  has_one :article, as: :art, dependent: :destroy
  accepts_nested_attributes_for :article, reject_if: :all_blank

  translates :slug
  extend FriendlyId
  friendly_id :title, use: :globalize
  globalize_accessors attributes: %i[slug]
  def title
    article.friendly_id
  end
end

我也尝试过彻底清除数据库并重新迁移它。 已清除缓存字段。

似乎没有任何效果。

微薄的请求

2.5.1 :004 > About.where('sort_index > ?',0).first
  About Load (0.6ms)  SELECT  "abouts"."id", "abouts"."created_at", "abouts"."updated_at", "abouts"."sort_index", "abouts"."anana" FROM "abouts" WHERE (sort_index > 0) ORDER BY "abouts"."id" ASC LIMIT $1  [["LIMIT", 1]]

 => nil 

columns_hash

2.5.1 :005 > About.columns_hash
 => {"id"=>#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x0000000003ec6690 @name="id", @table_name="abouts", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x0000000003ec6bb8 @sql_type="bigint", @type=:integer, @limit=8, @precision=nil, @scale=nil>, @null=false, @default=nil, @default_function="nextval('abouts_id_seq'::regclass)", @collation=nil, @comment=nil, @max_identifier_length=63>, "created_at"=>#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x0000000003ec5a88 @name="created_at", @table_name="abouts", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x0000000003ec5c18 @sql_type="timestamp without time zone", @type=:datetime, @limit=nil, @precision=nil, @scale=nil>, @null=false, @default=nil, @default_function=nil, @collation=nil, @comment=nil, @max_identifier_length=63>, "updated_at"=>#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x0000000003ec5588 @name="updated_at", @table_name="abouts", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x0000000003ec57e0 @sql_type="timestamp without time zone", @type=:datetime, @limit=nil, @precision=nil, @scale=nil>, @null=false, @default=nil, @default_function=nil, @collation=nil, @comment=nil, @max_identifier_length=63>, "sort_index"=>#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x0000000003eb7618 @name="sort_index", @table_name="abouts", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x0000000003eb7780 @sql_type="integer", @type=:integer, @limit=nil, @precision=nil, @scale=nil>, @null=true, @default=nil, @default_function=nil, @collation=nil, @comment=nil, @max_identifier_length=63>, "anana"=>#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x0000000003eb6f38 @name="anana", @table_name="abouts", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x0000000003eb71b8 @sql_type="integer", @type=:integer, @limit=nil, @precision=nil, @scale=nil>, @null=true, @default=nil, @default_function=nil, @collation=nil, @comment=nil, @max_identifier_length=63>} 

【问题讨论】:

  • 你的服务器重启了吗?
  • 是的,先生,我做了十几次。
  • 你能发布一些代码和输出来证明About.where('sort_index &gt; ?',0) 实际上返回一个空集吗?当您说About.where('sort_index &gt; ?',0).first 返回一个值时,您的下一个陈述似乎与此相矛盾。
  • 它返回 nil,我已将完整的控制台文本添加到问题中。您可以从那里查看。
  • About.columns_hash 返回什么?

标签: ruby-on-rails ruby ruby-on-rails-5


【解决方案1】:

所以问题基本上是我的sort_index默认为null,我设置后忘记保存它。

所以我有点担心地修复了它

module Sortable
  extend ActiveSupport::Concern
  included do
    default_scope { order(sort_index: :asc) }
    after_initialize do |obj|
      if obj.sort_index.nil?
        obj.sort_index = obj.id
        obj.save
      end
    end
  end
end

感谢大家的建议,这些东西困扰了我 6 个小时,哈哈 :)

【讨论】:

    【解决方案2】:

    您需要重新启动控制台或在控制台中运行reload!

    【讨论】:

    • 这件事也发生在网络服务器上。不是重新加载控制台。
    • active_record 有 min 方法吗?我尝试了 User.min(:id) 并得到了 method_missing 错误。然后尝试最低限度并工作
    • 不,我也尝试了最低限度,这是我在问题中的拼写错误。
    猜你喜欢
    • 2017-08-10
    • 2016-10-02
    • 2012-12-02
    • 2020-11-09
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 2020-08-18
    相关资源
    最近更新 更多