Monday, April 18, 2011

Alternative to "grep" command in Windows

Hi all,

I have shifted from ubuntu and started using windowsXP. When I tried to use the grep command in windows, it throws the error

Instead of using grep, we can use find
gem list|find "rails"

rails (3.0.10)

Cheers,
Vadivelan

Wednesday, February 2, 2011

Month with number of count display for any model in your application

By using rails finder method, it is very easy to display the number of post and month accordingly.
Here is the query

>> Post.count(:order => 'DATE(created_at) DESC', :group => ["DATE_FORMAT(created_at,'%m/%Y')"])
=> #< OrderedHash {"06/2010"=>1, "03/2010"=>1, "02/2010"=>1}>

Result is a collection hash ordered in the form of latest month as first one, Collection contains the month as Key and number of count as Value.

If we run it through the each loop and it is very easy to display.

Thanks,
Vadivelan

Monday, January 10, 2011

Ruby keywords

List of ruby keywords

1. alias
2. and
3. BEGIN
4. begin
5. break
6. case
7. class
8. def
9. defined
10. do
11. else
12. elsif
13. END
14. end
15. ensure
16. false
17. for
18. if
19. in
20. module
21. next
22. nil
23. not
24. or
25. redo
26. rescue
27. retry
28. return
29. self
30. super
31. then
32. true
33. undef
34. unless
35. until
36. when
37. while
38. yield

Sunday, January 9, 2011

SQL order_by "field" option

Hi all,

I tried to order the SQL results in a pre-defined order. Consider the case

Am having users table, and a field called 'status' to store the current status of the user.
In back-end of my site, am displaying all the users with order-by 'status'

Possible status values are "Pending", "Approved", "Canceled", "Deleted"

I need to display users in the following orders,
"Approved", "Pending", "Canceled", "Deleted"

The normal order by field orders the field in either ASC/DESC. But in this case it is different.
For this i used the following query to fetch SQL results in a defined order

Rails finder:
User.find_by_sql("select * from users order by field (status,'Approved', 'Pending', 'Canceled', 'Deleted')")

SQL query:
select * from users order by field (status,'Approved', 'Pending', 'Canceled', 'Deleted');

Result contains the collection in the defined order.

When you are using this "order by field" option, you have to give all the option in the order then only it will works.

Thanks,
Vadivelan

Wednesday, November 10, 2010

ActiveRecord::Base increment & decrement method

Hi all,

Yesterday while i was working in a task, i need to increase the points of an user. For that i have used the update_attribute method like below

user = User.last
user.update_attribute(:points, 5)

Then i found the active record base in-built method called "increment" and "decrement". So i used the following method to increment/decrement the user points.

user.increment(:points) # it will increase points by 1(default increment count) and returns the user object(self)
user.save
user.increment!(:points) # and this one will do the above two operations i.e., assigns the value to the attribute and saves the record. It will returns true or false(validation result).

user.increment(:points, 5) # we can pass the value like this to increment the attribute by this much. Here points will increases by 5
user.save
user.increment!(:points, 5)

The same is applicable to the "decrement" method, syntax is given below
user.decrement(:points)
user.decrement!(:points)
user.decrement(:points, 5)
user.decrement!(:points, 5)


Thanks,
Vadivelan

Wednesday, October 6, 2010

Use of rails console

Hi all,

We can use our rails console(powerful tool for rails app) for debugging and testing. Mainly console helps us to learn more about Ruby

Normally we are using console to talk with our database, and fetch the objects from db.
ex:
User.first
User.all

But apart from that we can use our console to interact with our application using the object "app"
"app"

app.class # returns this
ActionController::Integration::Session

we can fire(get/post) requests to our application from console itself.

>> app.get "/login"
=> 200
it returns the status code for the handled request

>> app.post "/user_sessions", :user_session => {:email => 'vadivelan@example.com', :password => 'secret'}
=> 302
we can send post request with parameters like this

>> app.response.redirect?
=> true
redirected to some other url

>> app.response.redirect_url
=> "http://localhost/login"
view the redirect url

Hope now you guys can start firing the requests to application from console.

Thanks,
Vadivelan

Friday, September 24, 2010

ruby send method

Hi all

I am started using the method named "send".

We can use this to call the method using object.

class User

def say_hello
"Hello"
end

end

u = User.new
puts u.send(:say_hello)

Here the class "User" is having the method called say_hello. We can create an object for the user class and call the method using the method "send"

Cheers,
vadivelan