【发布时间】: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 > ?',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>}
【问题讨论】:
-
你的服务器重启了吗?
-
是的,先生,我做了十几次。
-
你能发布一些代码和输出来证明
About.where('sort_index > ?',0)实际上返回一个空集吗?当您说About.where('sort_index > ?',0).first返回一个值时,您的下一个陈述似乎与此相矛盾。 -
它返回 nil,我已将完整的控制台文本添加到问题中。您可以从那里查看。
-
About.columns_hash返回什么?
标签: ruby-on-rails ruby ruby-on-rails-5