Understand heroku environments

Before deploy in production is always recommended to test everything first, probably you have a Test suit for test your application, but you never know want is going to happen when you deploy all changes to production. What people usually do is create a staging environment, that copies the production environment.

Basically you deploy first to staging and then test evrything, so after checking everything you may deploy to production with the confidence that everything works perfectly. Heroku as I said before is amazing it allow’s us to create multiple environments, Heroku provides us with many documentation to create our environments. Managing Multiple Environments for an App This post show us how to create different environments. The problem is if you have an existing App is not very clear how to do it, but I shall help you do it.

Read more

Self-referential Associations with rails 4

When I first heard about this concept I was a little bit confuse. I remenber when I was doing the tutorial Learn rails by Example by Michael Hart, excellent first start, at the end of the book we implemented a self referential associations. By that time the concept was to much for me, so I did some research. I found Railscast episode Self-referential Associations, it threw some light in the concept, but I still confuse.

Right now I think I understand the concept a little bit better but there always room for improvement. I’m going to show you the code for a simple self-referential association for friends and followers.

Read more

Recently reading the some posts about ruby I found myself with this interesting method call lazy. This method will help us when looping over some objects: Array, Hashes, Files ……. If you want to loop though an array that is huge, an we only want to select the first 20 element that evaluate our criteria, this is the kind of job for our lazy method.

Without Lazy

1
2
3
4
5
6
7
arr =  (1..1000).to_a
selected = []
arr.select do |x|
  selected << x if x.odd?
  break if selected.size > 20
end
p selected

This looks familiar but we may use the lazy method.

With Lazy method

1
2
3
4
5
arr =  (1..1000).to_a
selected = arr.lazy.select do |x|
  x if x.odd?
end.take(20).force
p selected

This way the syntax is more clear and is better in performance.

While reading the documentation of ActiveRecord::Associations, there are many useful methods, that I didnĀ“t know about it. But one came to me as quite useful, inverse_of. This method allow us to work with associations that haven`t been save yet, so it will work with the memory. This will optimise our object loading.

I will so you with some example.

1
2
3
4
5
6
7
Class Post < ActiveRecord::Base
  has_many :comments, inverse_of :post
end

Class Comment < ActiveRecord::Base
  belongs_to :post, inverse_of :comments
end
Read more

Heroku is AWESOME!

Recently I was playing around with my local database in my rails project, eventually I screw up, I had to delete all the data. I use rake db:resetand rake db:create but how I was going to get all the data again, manually ? No way.

Heroku is a great tool for deploying.

With three simple lines of code in your terminal you are able to download your production database and load it in your local envairoement.

In your terminal and in your project folder type:

1
2
$ heroku pgbackups:capture
$ curl -o latest.dump `heroku pgbackups:url`

This two line create a backup and read it to file call latest.dump.

1
$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d mydb latest.dump

With this last line we are restoring the database from the backup file, remember change myuser for your actual user and mydb for your database name.

Hope this helps.

Sending emails with rails and Heroku (Updated 2014-04-24)

Rails makes extremly easy to send emails, I’m not going to explain how to do it, there are pretty good tutorials around the internet, this one is really well explain: Action Mailer Basics.

It is common to delay the task in our rails applications, that way the app doesn’t stop. There are many good gems that help us, I usually use is delayed_job. This gems makes it really easy, we just have to prepend delay in our process Mailer.delay.sendMail(args).

In my actual project some of the mails we send, has attachments files, usually for that type of task it is common to create a temp file that store the information, and send it as arguments to mailer action.

Read more

Postgresql ORDER clause or be used in a aggregate function

While working in a rails project recently, I step on an error that say: PG::Error: ERROR: column "number" must appear in the GROUP BY clause or be used in an aggregate function, I didn’t have any idea how to solve it.

Reading for a while I think I have a small understand, why this error appear.

We have to specify which column should be use for the GROUP BY or for the ORDER inside our SELECT for example Sale.all.select(:ordered_on, :number).group(:ordered_on, :number).order(:number).

With this query , we are able to retrieve all sales group by ordered_on and number and order by it as well.

This error jump while using Postgresql because it is more restrictive than MySQL. So remember be careful if you are deploying to Heroku because it’s default database.

Hope this help you.

Here are some good references for the future. Postgresql Tutorial

Integrate your Octopress with Disqus

I’m new to Octopress platform for Blogging, but so far I think is great and simple.

Creating my first post I felt so excited, as when you discover a new treasure or a secret path in your favorite game. But everything at the beginning is not going to be smooth. My first problem was that my post weren’t displaying the Disqus comments, that are a very common system for comments in the blog community. After hours of searching the web and reading lot of post about the topic I had a goodunderstand of how to integrated them.

So I thought of sharing my experience:

Read more

Rails and select

In a rails project I’m working on I was trying to select from the database some sales with some conditions.

To start the Sale model has multiples associations .

1
2
3
4
5
6
class Sale < ActiveRecord::Base
  belongs_to :voucher
  belongs_to :client
  has_many :line_items, dependent: :destroy
  has_many :bills
end

So the goal of this select was to obtain all the Sales where the LineItems has express_checkout set it to true.

Read more

This is my first post, and I hope not the last.

While doing some exercise of the book Learn to Program of Chris Pine, trying to rename some files as part of some exercise, that was renaming a bunch of picture from a folder, I noticed a strange functionality.

Using the method File.rename(old_name, new_name) and after running my program it seems that the file haven’t been rename instead it has been deleted, but that’s not what happen. What ruby was doing is renaming and moving the file to my printing working directory. What really solve it was giving an absolute path to the method.

File.rename(old_name, "/Users/gus/Desktop/myImages/new_name")

Read more