hdknr’s posterous

 
Filed under

rails

 

Life is beautiful: Ruby on Railsの「えせMVC」の弊害

興味深い議論をありがとうございます。ちょうどDeliciousのhostlistに関連するスライドが入っていたので紹介します。238〜248ページに Model is-a ActiveRecord(s) はアンチパターンで Model has-a ActiveRecord(s) のほうが良いと説明されています。
http://www.slideshare.net/billkarwin/sql-antipatterns-strike-back?src=embed
(私自身は Model is-a ActiveRecord(s) しか経験が無いです)

Filed under  //   Opinion   rails  

Comments [0]

Life is beautiful: Ruby on Railsの「えせMVC」の弊害

もはや MVC が本当に正しいのかどうか見直す時期に来ているのでは?

本当にオブジェクト指向や MVC は生産性をもたらしたのだろうか? Java なんか単に複雑になっただけなんじゃないだろうか?そういう疑問はあっていいと思います。

今は最先端を行ってるのは PAC(Presentation Abstract Controller)だと確信します。

PAC は MVC の階層的アーキテクチャとみることができます。
これを採用しているソフトには Drupal、Firefox、Eclipse などがあります。誰も指摘しないけど。

Drupal は PAC であり、米アマゾンのウェブ開発カテゴリでの本のランキングに Drupal が入ってきていることからもその生産性の高さが伺い知れると思います。

Filed under  //   Opinion   rails  

Comments [0]

Panda - Open source video platform

Panda is an open source solution for video uploading, encoding and streaming.

Panda architecture diagram

Unlike other video platforms, Panda is not just a service for encoding your videos for the web; Panda handles the whole process. From the upload form to streaming, Panda takes control.

By providing an elegant REST API, Panda makes it completely painless to implement full video uploading, encoding and streaming functionality to your web application in a matter of hours.

  • Runs completely within Amazon's Web Services utilising EC2, S3 and SimpleDB.
  • Everything contained within one elegant Merb application.
  • Support for the encoding profiles which FFmpeg supports. They include FLV for flash and H264 for iPhone.
  • Panda gem for painless integration with Ruby on Rails and Merb.
  • Lovely little admin dashboard for managing your videos.

Filed under  //   rails   video  

Comments [0]

Ruby on Rails/ビューに雛形を使う - 俺の基地 (layout)

明示的に指定 Edit

複数のコントローラで共通で使うlayoutがある場合は明示的に指定したい。こういう場合はコントローラの定義が始まるところでlayoutメソッドをlayout名指定で呼んでやるとそれがコントローラのアクションにすべて適用される

class UsersController < ApplicationController
  layout "hoge"

この場合はUsersController内のすべてのアクションに

hoge.rhtml

のlayoutが適用される。

Filed under  //   layout   rails  

Comments [0]

フラグメントキャッシュをRailsで使う。 - kaeruspoon

 Railsでは三つのキャッシュ機能があります。ページキャッシュにアクションキャッシュ、そしてフラグメントキャッシュです。今回はフラグメントキャッシュを使ってみました。
 使いかたはとても簡単で、キャッシュしたいビューの部分をcacheブロックに入れるだけ。

Filed under  //   cache   fragment cache   rails  

Comments [0]

Rails 2.1の主な変更点を整理 - ひげろぐ

キャッシュ機構の強化 – Better caching

フラグメントキャッシュなどのキャッシュは今まではファイルに保存するしかなかったけれども、これをメモリやmemcacheやdrbなど他のキャッシュストアにも保存できるようになった。

定義はconfig/environment.rbなどで以下のように

ActionController::Base.cache_store = :memory_store
ActionController::Base.cache_store = :file_store, "/path/to/cache/directory"
ActionController::Base.cache_store = :drb_store, "druby://localhost:9192"
ActionController::Base.cache_store = :mem_cache_store, "localhost"

何も指定しないときのデフォルトは:memory_storeになる。

このほかにも自作のキャッシュストアを作成して使うこともできる。

参考

Filed under  //   cache   rails  

Comments [0]

ruby:MemCache

hdknr@debsq:~/tmp/cached_mem$ more config/environment.rb

memcache_options = {
:c_threshold => 10_000,
:compression => false,
:debug => false,
:namespace => 'vis_mod',
:readonly => false,
:urlencode => false
}
CACHE = MemCache.new memcache_options
CACHE.servers = 'localhost:11211'

hdknr@debsq:~/tmp/cached_mem$ ruby script/console
Loading development environment (Rails 2.3.4)
@@@ config/environtment.rb
>> sql ="SELECT * FROM `categories` WHERE (`categories`.`id` = 1)"
=> "SELECT * FROM `categories` WHERE (`categories`.`id` = 1)"
>> d = Digest::MD5.hexdigest(sql)
=> "1b837a35dbfd6bf42f95e110df28c9e5"
>> Category.find_by_sql(sql)
=> [#]
>> CACHE.set(d,Category.find_by_sql(sql))
=> "STORED\r\n"
>> CACHE.get(d)
=> [#]

memcachedログ。

32: Client using the ascii protocol
>32 STORED
>32 sending key vis_mod:1b837a35dbfd6bf42f95e110df28c9e5C 0 247
Category:@attributes_cache{
>32 END

Filed under  //   MemCache   memcached   rails   ruby  

Comments [0]

Ruby on RailsでRuby-GetText-Packageを使う (Rails-2.1.x以前) - よたらぼ 保管庫

ローカライズドテンプレートの使用

foo_ja.hml.erb, foo_ja_JP.html.erbと言う風にテンプレート自体を英語の元ファイルから分離させてしまうこともできます。例えば以下のようにファイルを配置します。

/app/views/blog/list.html.erb
/app/views/blog/list_ja.html.erb

このケースでは日本語(jaロケール)の場合のみ、2番目のファイルが呼び出され、それ以外の時は1番目のファイルが呼び出されます。 render_partialの場合は、_foo.html.erb, _foo_ja.html.erbとすると、日本語の場合のみ_foo_ja.html.erbが呼び出されます。

Note メンテナンス性を考えるとこの機能はなるべく使わないことを推奨します。 こちらにその理由がありますのでご参照ください。なお、この日記のエントリに書かれているソースコードはすでにRuby-GetText-Package本体に取り込まれていますので無視してください。

使わない方がいいのか。

Filed under  //   gettext   locale   rails   ruby  

Comments [0]

Module: ActiveRecord::Calculations::ClassMethods

count(*args)

Count operates using three different approaches.

  • Count all: By not passing any parameters to count, it will return a count of all the rows for the model.
  • Count using column: By passing a column name to count, it will return a count of all the rows for the model with supplied column present
  • Count using options will find the row count matched by the options used.

The third approach, count using options, accepts an option hash as the only parameter. The options are:

  • :conditions: An SQL fragment like "administrator = 1" or [ "user_name = ?", username ]. See conditions in the intro to ActiveRecord::Base.
  • :joins: Either an SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id" (rarely needed) or named associations in the same form used for the :include option, which will perform an INNER JOIN on the associated table(s). If the value is a string, then the records will be returned read-only since they will have attributes that do not correspond to the table‘s columns. Pass :readonly => false to override.
  • :include: Named associations that should be loaded alongside using LEFT OUTER JOINs. The symbols named refer to already defined associations. When using named associations, count returns the number of DISTINCT items for the model you‘re counting. See eager loading under Associations.
  • :order: An SQL fragment like "created_at DESC, name" (really only used with GROUP BY calculations).
  • :group: An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause.
  • :select: By default, this is * as in SELECT * FROM, but can be changed if you, for example, want to do a join but not include the joined columns.
  • :distinct: Set this to true to make this a distinct calculation, such as SELECT COUNT(DISTINCT posts.id) …
  • :from - By default, this is the table name of the class, but can be changed to an alternate table name (or even the name of a database view).

Examples for counting all:

  Person.count         # returns the total count of all people

Examples for counting by column:

  Person.count(:age)  # returns the total count of all people whose age is present in database

Examples for count with options:

  Person.count(:conditions => "age > 26")
  Person.count(:conditions => "age > 26 AND job.salary > 60000", :include => :job) # because of the named association, it finds the DISTINCT count using LEFT OUTER JOIN.
  Person.count(:conditions => "age > 26 AND job.salary > 60000", :joins => "LEFT JOIN jobs on jobs.person_id = person.id") # finds the number of rows matching the conditions and joins.
  Person.count('id', :conditions => "age > 26") # Performs a COUNT(id)
  Person.count(:all, :conditions => "age > 26") # Performs a COUNT(*) (:all is an alias for '*')

Note: Person.count(:all) will not work because it will use :all as the condition. Use Person.count instead.

Filed under  //   ActiveRecord   rails   ruby  

Comments [0]

countよりもcount_by_sqlの方がいいかも - おもしろWEBサービス開発日記

>> Blog.count
  SQL (862.4ms)   SELECT count(*) AS count_all FROM `blogs` 
=> 100000
>> Blog.count_by_sql("select count(*) from blogs force index(index_blogs_on_title)")
  Blog Count (75.7ms)   select count(*) from blogs force index(index_blogs_on_title)
=> 100000
>>
  • countのみは862.4ms
  • count_by_sqlは75.7ms

10倍以上の差がつきました。

Filed under  //   Performance   rails  

Comments [0]