Rails — Rewrite ActiveRecord results

Get results

results = Post.all

Rewrite

results.map do |result|
  [
    id,
    name + "rewrited"
  ]
end
Advertisements

Deploy Django Application to Heroku

Version

  • Python: 2.7.3
  • Django: 1.7

What to do on localhost?

Start from

Important thing is to run

python manage.py collectstatic

If you use public CDN at frontend and also use Django admin feauture,be sure to run it to show the admin style correctly after finishing your application.

Run Heroku commands

heroku run python manage.py sqlmigrate [your_app] 0001

The 0001 is the number of your migration.

heroku run python manage.py migrate

Until now,model tables have been created in your database.

Create superuser

heroku run python manage.py createsuperuser

That’s all.

Settings

https://github.com/zhangjingqiang/qiang-commands/blob/master/qiang_commands/settings.py

Ruby 中如何计算数组中哈希值的总和?

例如:

array = [{"a"=>1},{"b"=>2},{"c"=>3},{"a"=>2},{"b"=>4}]

变换成:

array = [{"a"=>3},{"b"=>6},{"c"=>3}]

方法如下:

array.inject(Hash.new{|h,k| h[k] = 0}){ |h, a| k, v = a.flatten;  h[k] += v; h }.map{|arr| Hash[*arr] }
#=> [{"a"=>3}, {"b"=>6}, {"c"=>3}]

or

array.each_with_object(Hash.new{|h,k| h[k] = 0}){ |a, h| k, v = a.flatten;  h[k] += v }.map{|arr| Hash[*arr] }
#=> [{"a"=>3}, {"b"=>6}, {"c"=>3}]

Detail