What is attr_accessor in Rails?
attr_accessor is used to define an attribute for object of Model which is not mapped with any column in database. This answers question - What is attr_accessor in Rails.
1. attr_accessor Methods
attr_accessor creates two methods i.e. getter method and setter method.
**getter method **- it is used to retrieve value of the attribute
setter method - it is used to set value to the attribute
When to use attr_accessor in Rails?
Whenever you want to show certain attribute for particular form but you do not want to store it’s value in the database then you use attr_accessor for that model as follows,
Suppose we have a Model called user in out database with following fields,
- id
- name
- age
- city
- profession
And we define attr_accessor :type_of_user as follows,
Now, whenever you create a object of the class User then you can assign a value to it’s attr_accessor.
This will create new object of class User and assign given attribute values. You can assign value to attr_accessor variable as follows,
This assigns ‘middle-aged’ value to the attribute type_of_user attr_accessor.
getter and setter in Action For attribute type_of_user getter and setter methods are created as follows,
And these are used whenever we assign or retrieve values from the attr_accessor.
- Usage in Classes Whenever changing value of the attr_accessor variables, we need to use self.variable_name = to assign new value to the variable. Otherwise, it will consider it as a local variable resulting into incorrect values.
For example,
Here, it the above service class we have defined name as the attr_accessor. We need to change it’s value based on some condition. And we return it’s value from process method.
When we execute this class as,
We can see that process method has returned nil.
Explaination
In the block,
As the if condition is false, and value of name is returned from the process method, name is considered as the local variable. Local variable name does not have any value. Thus, it returns nil.
Correction
To correct the above service class, we need to use explicity self when assigning value to the attr_accessor variable as given below,
Now if we execute this class, we will get expected result as given below.
- Lifetime of attr_accessor attr_accessor is accessible throughout object’s lifecycle. When object is saved using any of ActiveRecord method, save, update etc. all attributes of Model other than attr_accessor are saved in the database.
How to Access Throughout lifecycle of object, you can access the value of attribute accessor as follows,
- Conclusion We learned what is attr_accessor in Rails and how to use it with Rails .
What's new in Ruby 2.5?
Ruby 2.5.0-preview1 Released
We are pleased to announce the release of Ruby 2.5.0-preview1.
Ruby 2.5.0-preview1 is the first preview release toward Ruby 2.5.0. It introduces some new features and performance improvements, for example:
After installing the beta version through rvm …
The 2.5.0-preview1Ruby language version has just been released. Because it is not a stable version, its use is not yet recommended .
** Inverse backtrace**
The backtrace (list of methods that were run before an error happened) will start to be displayed in reverse order. Using the following code as an example:
if the same file is run in ruby 2.4 you get…
Rescue / else / ensure will be allowed inside of / end blocks
Running with Ruby 2.4:
Currently we can only capture exceptions within methods or blocks begin/ end, ie:
: Output Error caught: boom
: Output Error caught: boom.
But the code below doesn’t work:
Running with Ruby 2.5:
In the version that is about to be released, this is possible. The output of the above code will be:
New method yield_self
According to the official documentation Ruby 2.5
- Yields self to the block and returns the result of the block.
Although very similar to the tapclass method Object(which is very useful, by the way), your returns are different.
While with the method tapyou open a block and at the end the object itself is returned, with the yield_self, the result of the block is returned.
Example:
The save
method was called,
but object
is returned
I could not think of a practical case to use it, but certainly had a reason to have been added :)
Other changes
The new version will also bring other new features, such as support for Unicode version 10, which includes 56 emojis created this year . You can see a complete list with all the news in this document .
To install Ruby 2.5 (though not yet recommended for not being a stable release) through RVM, simply run:
rvm install 2.5.0-preview1
for more information visit Ruby 2.5.0-preview1 Released
Yes!!! Enjoy Ruby 2.5.0-preview1 Released
redirect_to(options = {}, response_status = {}) protected
Redirects the browser to the target specified in options. This parameter can take one of three forms:
- Hash - The URL will be generated by calling url_for with the options.
- Record - The URL will be generated by calling url_for with the options, which will reference a named URL for that record.
- String starting with protocol:// (like http://) - Is passed straight through as the target for redirection.
- String not containing a protocol - The current protocol and host is prepended to the string.
- :back - Back to the page that issued the request. Useful for forms that are triggered from multiple places. Short-hand for redirect_to(request.env["HTTP_REFERER"])
Examples:
The redirection happens as a “302 Moved” header unless otherwise specified.
Examples:
It is also possible to assign a flash message as part of the redirection. There are two special accessors for commonly used the flash names alert and notice as well as a general purpose flash bucket.
Examples:
When using redirect_to :back, if there is no referrer, RedirectBackError will be raised. You may specify some fallback behavior for this case by rescuing RedirectBackError.
Using Delayed Jobs in Rails
When creating a web application sometimes need arises that we have to schedule some jobs during the execution of a particular method. Due to these job the render of the page has to wait until the task gets over. This waiting can be sometimes annoying because a user who is waiting for a job to get completed will have to wait for a lot of time if the number of jobs to be performed is more. For example if we take the scenario of sending mails to the user. Now for this if we have large number of users to whom we want to send a mail from our application so it will take a lot of time to get completed which no one will like to wait. So by using delayed jobs what we can do is we can execute the processes in the background and user wont have to wait till all the processes are completed. For that we will be using a gem called
place this gem in your gemfile and run bundle install. Now after done with this we need to add the generator of this gem because active record actice records backend requires a job table in which the jobs will be stored in a queue. You can create the table by running the following command
and then run rake db:migrate Now when using this in development mode we need to set the queue_adapter in config/application.rb
with the help of this queue adapter we dont need to restart delayed Job every time we update our code in development. Now once done with this we will have to start the rake job in our console which will allow the jobs to run in the background without having the need to restart it again and again. We need to write this in console
This will start the rake job thread and will wait for a process or a task to come to it. Now last thing which we need to do is call the delay method where we actually need our jobs to be processed in background without affecting the current execution. Suppose oif you are sending a mail to all the users who have subscribed for a magazine so the users wont have to wait until the mail is sent to all the users. we will just add the delay method like this
Here notification mailer is the name of the mailer and notification_email is the method through which the mail is being sent. Now the mail will be sent to all users in the background and the execution of the method wont be delayed.
Before_validation and after_touch callbacks in Rails
Rails provide us with some pre-defined methods or hooks which can be invoked after or before the execution of a certain method in the object life cycle. Callbacks are called in certain moments of object’s life cycle which we will get to know in this blog. Today we will be studying about three callbacks as follows : before_validation As the name suggests, this method is called before validation of all the fields of the form. For example, there is a registration form which has an email field, so as soon as the user will fill the email field and submit the form before the value gets saved into the database we have to make sure that all characters in the email should be down case. So in this case, we will invoke a before_validation callback in which we will define a method to be called before the create method is called. In this method, we will convert the whole string into down case and after that, the create method will be called like this.
class Employee < ApplicationRecord
before_validation :normalize_email, on: :create
# :on takes an array as well
def create
...
end
protected
def normalize_email
self.email = email.downcase.titleize
end
end
after_touch Whenever an object is being touched the after_touch callback is called . For example We create an employee object and as soon as we touch the object the after_touch callback is invoked.
class Employee < ApplicationRecord
after_touch do |employee|
puts "The object has been touched"
end
end
>> e = Employee.create(name: 'Tom')
=> #<Employee id: 1, name: "Tom", created_at: "2013-11-25 12:17:49", updated_at: "2013-11-25 12:17:49">
>> e.touch
The object has been touched
=> true
So hope this article was helpful to understand both the callbacks.
subscribe via RSS