百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

nilify_blanks

> 编程语言
开源

为在使用 DB NULL 而不是简单的空字符串的情况下,将输入的空值保存为 nil 在数据库中提供了一个框架。

328 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

为在使用 DB NULL 而不是简单的空字符串的情况下,将输入的空值保存为 nil 在数据库中提供了一个框架。

{}[http://travis-ci.org/rubiety/nilify_blanks]

== Nilify Blanks

In Rails when saving a model from a form and values are not provided by the user, an empty string is recorded to the database instead of a NULL as many would prefer (mixing blanks and NULLs can become confusing). This plugin allows you to specify a list of attributes (or exceptions from all the attributes) that will be converted to nil if they are blank before a model is saved.

By default, columns set as NOT NULL will not be nilified, since that value cannot be persisted, and it might be better to catch that with a validation. If you want to nilify those columns anyway, you can use only: (see below) to explicitly mention them.

Only attributes responding to blank? with a value of true will be converted to nil. Therefore, this does not work with integer fields with the value of 0, for example. Usage is best shown through examples:

== Requirements

As of v1.4.0, this gem requires Rails 4 and Ruby 2.2 or higher.

== Install

Include the gem using bundler in your Gemfile:

gem "nilify_blanks"

== Basic Usage

Checks and converts all content fields in the model

class Post < ActiveRecord::Base nilify_blanks end

Checks and converts only text fields in the model

class Post < ActiveRecord::Base nilify_blanks types: [:text] end

Checks and converts only the title and author fields

class Post < ActiveRecord::Base nilify_blanks only: [:author, :title] end

Checks and converts all fields except for title and author

class Post < ActiveRecord::Base nilify_blanks except: [:author, :title] end

Checks and converts any fields, regardless of their null constraint

class Post < ActiveRecord::Base nilify_blanks nullables_only: false end

== Global Usage

You can also apply nilify_blanks to all models inheriting from ActiveRecord::Base:

ActiveRecord::Base.nilify_blanks

Or perhaps just a model namespace base class:

Inventory::Base.nilify_blanks

== Specifying a Callback

Checking uses an ActiveRecord before_validation callback by default, but you can specify a different callback with the :before option. Any callback will work - just first remove the "before_" prefix from the name.

class Post < ActiveRecord::Base nilify_blanks before: :create end

class Post < ActiveRecord::Base nilify_blanks before: :validation_on_update end

== RSpec Matcher

First, include the matchers:

require "nilify_blanks/matchers"

To ensure for a given column:

describe City do it { should nilify_blanks_for(:name) } end

To ensure for all applicable content columns:

describe City do it { should nilify_blanks } end

You can optionally match on options also:

describe City do it { should nilify_blanks_for(:name, before: :create) } end

describe City do it { should nilify_blanks(before: :create) } end

== Running Tests

This gem uses appraisal to test with different versions of the dependencies. See Appraisal first for which versions are tested, then run to test all appraisals:

$ rake appraisal install $ rake appraisal test

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言