Note to self.

When deploying my RubyOnRails applications with Capistrano and I get errors like this one

1
2
3
4
5
servers: ["app.myserver.com"]
[app.myserver.com] executing command
** [out :: app.myserver.com] sh: bundle: command not found
command finished in 31ms
servers: ["app.myserver.com"]    [app.myserver.com] executing command ** [out :: app.myserver.com] sh: bundle: command not found    command finished in 31ms

Capistrano executes the commands in a very basic shell so the $PATH environment are not the same as when you login through SSH.

One way to solve this is by setting PermitUserEnvironment to yes in /etc/sshd_config and then add the correct PATH to ~/.ssh/environment

I just found another way that was much easier :-)

In you config/deploy.rb add the following snippet

1
2
3
set :default_environment, {
  'PATH' => "/opt/ruby-enterprise/bin/:$PATH"
}

Thats it!

Cheers!

Posted in Programming, Rails, Ruby at June 1st, 2011. 1 Comment
Tagged with , , . Written by:

Recently I’ve been working a lot with dates and tonight I had a hard time finding information about how to group a model by date when the model only have a timestamp column.

Lets pretend we have a user model with a created_at attribute which is stored as a timestamp.

If we want to plot a simple graph showing signups per date this is the way to extract them:

1
User.count(:group => "DATE(created_at)")

Thats all there is to it. From this you will get an OrderedHash containing the date and count of users for each date.

One small thing to remember if you are using PostgreSQL. You will need the DATE() function in any ORDER/SELECT statements because PostgreSQL will only select/order by fields thats in the GROUP BY statement and this is a pretty healthy behavior that many databases (no names here) don’t care about. Think about it. If you are grouping all users by date and use * as selector how would the database know which username or email address to display?

This is what happens if you try to group by created_at without DATE() ->

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>> User.count(:order => 'created_at DESC', :group => ["DATE(created_at)"])                                                
ActiveRecord::StatementInvalid: PGError: ERROR:  column "users.created_at" must appear in the GROUP BY clause or be used in an aggregate function
: SELECT count(*) AS count_all, DATE(created_at) AS date_created_at FROM "users"  GROUP BY DATE(created_at)  ORDER BY created_at DESC
        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:212:in `log'
        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:507:in `
execute'
        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:985:in `select_raw'

        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:972:in `select'
        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `
select_all_without_query_cache'
        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'

        from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:255:in `execute_grouped_calculation'
        from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:132:in `
calculate'
        from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:130:in `catch'

        from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:130:in `calculate'
        from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:48:in `
count'
        from (irb):18

And this is the right way to do it:

1
2
>> User.count(:order => 'DATE(created_at) DESC', :group => ["DATE(created_at)"])
=> #<OrderedHash {"2009-10-14"=>22, "2009-10-25"=>4, "2009-11-04"=>8, "2009-10-15"=>9, "2009-10-26"=>16, "2009-11-05"=>9, "2009-10-16"=>193, "2009-10-27"=>14, "2009-11-06"=>9, "2009-10-17"=>49, "2009-10-28"=>15, "2009-11-07"=>6, "2009-10-18"=>36, "2009-10-29"=>8, "2009-10-19"=>116, "2009-10-30"=>15>

As you may notice the Hash does not look ordered. But if you loop over it you will get them in order:

1
2
3
4
5
6
7
8
9
10
11
12
>> User.count(:order => 'DATE(created_at) DESC', :group => ["DATE(created_at)"]).each {|u| puts "#{u[0]} -> #{u[1]}" }
2009-11-07 -> 6
2009-11-06 -> 9
2009-11-05 -> 9
2009-11-04 -> 8
2009-11-03 -> 14
2009-11-02 -> 20
2009-11-01 -> 10
2009-10-31 -> 6
2009-10-30 -> 15
2009-10-29 -> 8
..

And thats how you group a timestamp field with just its date part.

Cheers!

Posted in Databases, PostgreSQL, Rails, Ruby at November 8th, 2009. 3 Comments
Tagged with , , , , . Written by:

Today I had a really painful experience with rails InvalidAuthenticityToken. It turned out not to have anything to do with rails at all and there is where the painful part come in to play.

Read More…

Posted in Hosting, Programming, Rails at March 23rd, 2009. 1 Comment
Tagged with , , , . Written by:

Just before christmas I worked with one of our customers new server cluster or cloud if you may.
This cluster has production nodes and staging nodes and this post is about how to get capistrano to play nice with both production and the staging environment.
Read More…

Posted in Hosting, Programming, Rails, Ruby at January 6th, 2009. 8 Comments
Tagged with , , , . Written by:

Just released a new version of capistrano_colors. This is a total rewrite from the last version. This post show some of the bigger changes.
Read More…

Posted in gems, Programming, Projects, Rails, Ruby at January 4th, 2009. No Comments
Tagged with , , , , , . Written by:

Today I got the chance to give Ruby Enterprise Edition a try. We are currently working with a really fun new environment and you do not get better opportunities to try new stuff as when you have a complete new environment thats not in production. So we gave REE a shoot. When compiling REE with got in trouble really fast and this post is about the quick fix to solve it and some of our early performance results.

Read More…

Posted in Hosting, Programming, Rails at December 15th, 2008. 9 Comments
Tagged with , , , , . Written by:

Finally my friends at newsdesk have a blog. They have started off real good with a comparison of three ruby wrappers for Google charts.

I have great expectations from the newsdesk developers blog. If you are a Ruby and Rails developer you should definitely keep and eye on them!

Posted in Life, Rails, Ruby at November 25th, 2008. No Comments
Tagged with , , , , . Written by:

Today one of our server got in trouble with an ACE encoded IDN domain. The problem seemed to be rails in combination with nginx’s X-Sendfile feature. The result became that when accessing the site trough the IDN domain x-senfile did not send any files….

Our quick and dirty solution (until we have found the source/or solved the problem) became redirecting all traffic from the IDN domain to the domain without strange-swedish-characters.

With nginx this is as simple as in most webservers just put the following code before you original server configuration in nginx.conf.

1
2
3
4
server {
    server_name  xn--svenskdomn-y5a.se www.xn--svenskdomn-y5a.se;
    rewrite ^(.*) http://www.svenskdoman.se$1 permanent;
}

And Bowang! All requests to www.svenskdomän.se will be redirected to www.svenskdoman.se.

Domain names have been changed to protect the innocent :-)

Cheers!

Posted in Hosting, Rails at May 21st, 2008. No Comments
Tagged with , , . Written by:

pastbedti.me is using WP-Gravatar