Fast-track your Web apps with Ruby on Rails
To delete a particular record we will go for delete or destroy methods.But according to the names both looks very similar, but the functionality wise both are different. so lets check the difference between delete and destroy methods in Active Record.
Now first check what the delete method will do:
Delete method deletes the row with a primary key matching the id argument, using a SQL DELETE statement, and returns the number of rows deleted. Active Record objects are not instantiated, so the object‘s callbacks are not executed, including any :dependent association options or Observer methods.
You can delete multiple rows at once by passing an Array of ids.
It is much faster than the destroy method, skipping callbacks might bypass business logic in your application that makes the function to work fast.
example:
Delete a single row
user.delete(1)
Delete multiple rows
user.delete([2,3,4])
Destroy method:
The destroy method makes the SQL call to the database and destroys the row in the table that contains it. It does still allow you to manipulate the object in the application as long as it’s still in scope (i.e) the callbacks and
filters are allowed even after destroying the object.
example :
Destroy a single object
User.destroy(1)
Destroy multiple objects
users= [1,2,3]
User.destroy(users)
Category : Ruby on Rails
Rails 3:
Rails is a framework to develop web applications in Ruby. And the web application in Rails will be simpler and faster than in other languages. As, rails is an opinionated software, in the development process of an application, rails will do assumptions about our requirements and will generate the required code for the application at various stages, with a little interaction of the developer for few specifications.
Rails has a philosophy includes the following:
1) DRY: (Don't Repeat Yourself)
In rails, it suggests that writing the same code again and again for several times is not a good process. Instead of writing the same code again and again, we can partition that particular part of code in to a seperate block or function or something else and can reuse it whenever and wherever we need it in the current application.
2) Convention over Configuration:
In rails, almost the process of development will be done using the conventions instead of a * configuration settings for our application. If we follow the conventions (guidelines of rails) in development process, everything will be taken care by rails about our application.
3) REST: (Representational State Transfer)
In rails, REST will have two main features. They are:
a) Using resource identifiers like URLs to represent the resources of our application.
b) Transferring the representations of the state of a particular resource among system components.
REST will be useful for routing in our applications for the specification of path for a particular resource. And REST consists of HTTP verbose for our application like:
a) POST b) GET c) PUT d) DELETE.
MVC architecture:
The MVC architecture in Rails application has:
1) Model:
Model will consist of the data about our applications and is used to manipulate the content of models (tables). It will contain the validations, rules and set of guidelines to manipulate our models.
2) View:
View will consist of the templates of html pages, which will be visible for public/user. It will purely concentrate on the representation of data our application.
3) Controller:
Controller is the thing which will have the control over both Model and View. The Controller will receive the requests from the user (from views) and will send that request to respective model to have the information, and will pass the data to the users in form of a view.
The representation of MVC Architecture will be as follows:
View————————–Controller———————–Model
(User interface)————–(Controllers)——————(Databases)
Category : Featured, Ruby on Rails
Mutable objects :
1)Mutable objects are objects whose state can change.
2)In Ruby, Mutability is a property of an instance.
3)Any instance can become immutable by calling the "freeze".
Immutable objects :
1)Immutable objects are objects whose state never changes after creation.
2)Immutable objects are thread-safe(A piece of code is called as thread-safe ,if it functions correctly during simultaneous execution by multiple threads).
Freezing Objects:
1:The freeze method in class Object prevents you from changing an object, effectively turning an object into a constant.
2:After we freeze an object, an attempt to modify the freezed object, results in TypeError.
3: freeze operates on an object reference, not on a variable.
4:This means that any operation resulting in a new object.
Example:
str = 'A simple string. '
str.freeze
begin
str << 'An attempt to modify.'
rescue => err
puts "#{err.class} #{err}"
end
# The output is – TypeError can't modify frozen string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | def get_video_file_duration(inputFilename) command = "ffmpeg -i " + inputFilename.to_s + " 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//" output = `#{command}` if output =~ /([\d][\d]):([\d][\d]):([\d][\d]).([\d]+)/ duration = (($1.to_i * 60 + $2.to_i) * 60 + $3.to_i) * 10 + $4.to_i end #return duration.to_s return "#{$2}:#{$3}" end |

Category : Ruby on Rails
display_flash(:error) # to display a specific flash message
display_flash # to display all flash messages
1 2 3 4 5 6 7 8 9 10 11 12 13 | # Methods added to this helper will be available to all templates in the application. module ApplicationHelper FLASH_TYPES = [:error, :warning, :success, :message] def display_flash(type = nil) html = "" if type.nil? FLASH_TYPES.each { |name| html << display_flash(name) } else return flash[type].blank? ? "" : "#{flash[type]}" end html end end |
1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321
1 x 9 + 2 = 11
12 x 9 + 3 = 111
123 x 9 + 4 = 1111
1234 x 9 + 5 = 11111
12345 x 9 + 6 = 111111
123456 x 9 + 7 = 1111111
1234567 x 9 + 8 = 11111111
12345678 x 9 + 9 = 111111111
123456789 x 9 +10= 1111111111
9 x 9 + 7 = 88
98 x 9 + 6 = 888
987 x 9 + 5 = 8888
9876 x 9 + 4 = 88888
98765 x 9 + 3 = 888888
987654 x 9 + 2 = 8888888
9876543 x 9 + 1 = 88888888
98765432 x 9 + 0 = 888888888
Category : Ruby on Rails
Mod_rails has come to rescue people form tedious rails application development. With mod_rails the work has become a lot easier, you do not have to worry about different things just few steps and your application is up and running, ofcourse there are options to modify the deployment to your needs.
There are few prerequisites for mod_rails to work, first you need Apache and secondly you need Ruby and Rubygems.
Step 1: We need to update rubygems
sudo gem update
Step 2: We need to install mod_rails(passenger)
sudo gem install passenger
Step 3: Now we need to install Apache2 headers
sudo aptitude install apache2-prefork-dev
Step 4: Now we need to install Apache2 module
sudo passenger-install-apache2-module
There will be a screen which takes you further. If there are any dependencies that are needed then it will be asked for on this screen.
After successful installation there is just one thing left, we need to copy some lines to the Apache2 main config file , open the file “apache2.conf” in a text editor and then paste these lines at the end of the file:
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-1.0.4/ext/apache2/mod_passenger.so
RailsSpawnServer /usr/lib/ruby/gems/1.8/gems/passenger-1.0.4/bin/passenger-spawn-server
RailsRuby /usr/bin/ruby1.8
Restart Apache
sudo service apache2 restart
Done!!





