Rails 4.2 beta has been released and final version is going to come out soon.
Listed are some feature are going introduced in version of Rails 4.2. You can also try them while it’s beta.
But apart from these there are more features which are going to ship in Rails 4.2.
Few of them are..
required
option for singular associations (belongs_to
and has_one
)
Sometime we need to validate presence of associated object not the foreign key used to map the association
Lets say for each user there must be a account so we validate it like this –:
1 2 3 4 |
|
But in Rails 4.2 we just need to set required
option to true which will validate associated object is present
1 2 3 4 |
|
required
option in generator for model and migration
When we generate model we can pass required
option for references/associations
1
|
|
It will automatically set required
true for commentable
association
1 2 3 |
|
And in migration set null: false
for commentable
association which is basically a database validation
1 2 3 4 5 6 7 8 9 |
|
We can also pass it with polymorphic
option if we need polymorphic relation
1
|
|
This required
option can also be passed in generating migration
1
|
|
validate and validate!
validate
It runs all validations and returns true
if no error found otherwise return false
.
It is alias of valid?
method.
validate!
It runs all validations and returns true
if no error found otherwise
raise ActiveRecord::RecordInvalid
if any validation fails
with_options
without explicit receiver
In with_options
block we have to pass explicit receiver whether it is required or not
1 2 3 4 5 6 |
|
Now in Rails 4.2 we don’t need to pass explicit receiver to with_options
until it is required
1 2 3 4 5 6 |
|
Touch multiple attributes
Before Rails 4.2 we can just pass one attribute in touch
method
1 2 3 |
|
But in Rails 4.2 we can update multiple attributes at once with touch
method
1 2 |
|
Support for PostgreSQL citext data type
Rails 4.2 added support for citext
column type in PostgreSQL adapter.
But we have to enable extension before running migration because its additional supplied module
1
|
|
citext
column type provides a case-insensitive character string type.
For example we have a users
table with bio column as citext
type
We have a user object with bio value as ‘developer’ but we search with ‘Developer’ it will return our user object in result
1 2 3 |
|
Internally citext
calls lower when comparing values so we don’t need to explicitly convert value in lowercase
1
|
|
Empty your database
Rails provides us few database related rake tasks such as to create/drop database and to run migrations.
There is a new rake task added rake db:purge
to empty database for current environment.
It removes your data and tables from database and of course we can pass environment RAILS_ENV
like any other rake task.
New binstubs(bin/setup)
We always need some set of commands to bootstrap our application.
Now in Rails 4.2 there is default bin/setup
script where we can have all tasks to setup our application in quick and consistent way. It is located in bin directory.
There are some defaults commands given by Rails but we can add more as per our requirement.
Transform Hash Values
To modify Hash values call transform_values
it accepts a block and apply the block operation to each value of hash
1 2 |
|
There is also a bang version transform_values!
which change original hash
1 2 3 |
|
Truncate String by words
There is new method truncate_words
which truncate a string by given number of words length
1
|
|
Pretty print for ActiveRecord object
Now in Rails console/logs we can print ActiveRecord object output nicely
just pass that object to pp
method
1 2 3 4 5 6 7 8 9 10 |
|
Skip gems
We can skip default gems to Gem file while creating new app with --skip-gems
option. They will not be added to our Gemfile
1
|
|
References
Here are some more articles about rails 4.2
- http://edgeguides.rubyonrails.org/4_2_release_notes.html
- http://blog.newrelic.com/2014/04/25/ponies-rails-adequaterecord/
- http://blog.plataformatec.com.br/2014/07/the-new-html-sanitizer-in-rails-4-2/
- http://www.justinweiss.com/blog/2014/08/25/the-lesser-known-features-in-rails-4-dot-2/
Thanks to all Rails contributors for making Rails awesome :)
If you any feedback on this article let me know @raysrashnmi on Twitter