All Fuzzy

Month

June 2012

7 posts

Reset Heroku production database

I tried a lot of different ways to wipe out Heroku production database. None of it worked. For example, these two are ones I tried that did not work.

$ heroku pg:reset
!    Usage: pg:reset DATABASE

$ heroku pg:reset SHARED_DATABASE --confirm minforum
Resetting SHARED_DATABASE (DATABASE_URL)... failed
!    Resource not found

The only way that worked for me was this.

$ heroku run rake db:reset

You don’t have to migrate the database after because db:reset does this for you. However, it is a good idea to restart Heroku.

$ heroku restart
Jun 29, 2012
#Heroku #ruby on rails
Why use JavaScript "Double Negative (!!)" trick? → stackoverflow.com

!! ensures that falsey values are returned as false.

Jun 23, 2012
#javascript #programming
Jun 22, 20122 notes
#seattle
Finished School

I attended the convocation last Saturday officially marking the end of my undergraduate studies in University of Waterloo. I studied electrical engineering with option in management science. My interests were in control systems and power systems. But I never knew my side interest in programming and web development would overtake my main skill set in EE.

This is good and bad. It’s bad in a sense that I have to relearn a lot of things other CS grads knew all these years. I have to be aware of areas of deficiency and catch up as soon as possible. But it’s good in a sense that I finally found what I like to do. I no longer have to search for my passion and finally focus on what I am interested in.

Hopefully in 5 years or so, I can see myself as a competent developer.

Jun 19, 20121 note
#career
Common Ruby methods on collections

Blocks in Ruby are methods without a name. Ruby comes with many methods which you can apply on collections. In most cases, you would not use for loops in Ruby.

Here are some common methods to get you started.

each - executes block and returns the list of objects without mutating

# prints 246810 and returns [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5].each {|x| print x*2}

map - executes block and returns the list of mutated objects

[1, 2, 3, 4, 5].map {|x| x*2}
# => [2, 4, 6, 8, 10] 

select - returns a list of objects when condition is true

[1, 2, 3, 4, 5].select {|x| x==2}
# => [2] 

reject - returns a list of objects when condition is false

[1, 2, 3, 4, 5].reject {|x| x==2}
# => [1, 3, 4, 5] 

uniq - returns a list without duplicates

[1, 2, 3, 3, 3, 4].uniq 
# => [1, 2, 3, 4] 

reverse - reverse the list

[1, 2, 3, 4].reverse
# => [4, 3, 2, 1] 

compact - return all non-nil objects

[1, 2, 3, nil, nil, 4].compact
# => [1, 2, 3, 4] 

flatten - flatten inner arrays

[[3,2], [4,4]].flatten
# => [3, 2, 4, 4] 

partition - Create two collections. First collection for true, second for false.

[1, 4, 5, 6].partition {|x| x==4||x==5}
 => [[4, 5], [1, 6]] 

sort without argument - Sorts the list

[31, 34, 11, 23, 1, 3].sort
# => [1, 3, 11, 23, 31, 34]
Jun 11, 20121 note
#ruby #programming
ActiveRecord methods not working well on Heroku?

I had trouble with ordering the query in a specified manner. It was working well on development environment, but on the production side with Heroku, ordering wasn’t working at all.

So I just tried…

heroku restart
heroku rake db:migrate

And bam! Ordering magically works now.

Jun 10, 2012
#ruby on rails #heroku
Ruby on Rails - Heroku NoMethodError (undefined method `some_column` for ... )

Heroku was acting up on a problem that I didn’t encounter on a developing environment. Apparently, when you create an attribute on a model, it does not come with internally usable setter for that attribute. Here is the explanation for problem I had.

I have a model called Topic. A Topic has a column called last_post_id. In topics_controller, I had to use setter method for last_post_id to assign it a value. See the line @topic.last_post_id = Post.count + 1

def create
  @forum = Forum.find(params[:forum_id])
  @topic = @forum.topics.build(params[:topic])
  @topic.last_post_id = Post.count + 1
  if @topic.save
    flash[:success] = "Success!"
    redirect_to topic_posts_path(@topic)
  else
    render 'new'
  end
end

I thought that a setter and an accessor should be automatically available once you create an attribute of a model, but on Heroku, this doesn’t work.

So I had to make accessor and setter for last_post_id available in Topic model.

class Topic < ActiveRecord::Base
  ...
  attr_accessible :last_post_id
  ...
end
Jun 10, 2012
#ruby on rails #heroku
Next page →
2012 2013
  • January 1
  • February
  • March 4
  • April 5
  • May 19
  • June 5
  • July
  • August
  • September
  • October
  • November
  • December
2012 2013
  • January
  • February 1
  • March 4
  • April 2
  • May 2
  • June 7
  • July 4
  • August 4
  • September 5
  • October 10
  • November 10
  • December 4