嘿,我遇到了一个很奇怪的问题。我无法在模型的新添加列上使用任何查询。
这是东西,我有这个模型;
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 > ?',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 > ?",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>}
答案 0 :(得分:0)
所以问题基本上是我的sort_index默认为空,设置后我忘记保存它。
所以我稍加担心就解决了
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个小时:)
答案 1 :(得分:-2)
您需要重新启动控制台或在控制台中运行System.out.println(typeArg);
class java.lang.String
。