rails

has_manyのメソッドの戻り値

class User < ActiveRecord::Base belongs_to :group end class Group < ActiveRecord::Base has_many :users end っていうモデルを作った場合、 >> g1 = Group.find(:first) => #<Group id: 1, ・・・> >> users = g1.users => [#<User id:1m ・・・>] >> users.class => Arrayって出ます。しかも、 </user></group>…

with_scopeでいいじゃん

Railscastを見直していて、自分の変な思い込みに気づきました。 class Task < ActiveRecord::Base def self.find_incomplete find(:all, :conditions => ["complete = ?", false]) end end のfindのオプションを渡したい場合に class Task < ActiveRecord::B…

Git and Lighthouse 

We窶况e been preparing for Rails to move the official source repository from Subversion to Git for some time now and it seems that it窶冤l happen over the next week or so. The premiere will happen alongside the official launch of Github.Th…

id順、カラムの並び順でdump

ar_fixturesのrake db:data:dumpやdb:fixtures:dumpではYAMLの出力順は実行されたSQLやテーブルのカラムの並び順とは関係ないので、全部バラバラになってしまって、レコードがたくさんあったり、カラムが多い場合にすごく読みにくいです。いちいち目で探した…

Array#extract_options!

# Extract options from a set of arguments. Removes and returns the last element in the array if it's a hash, otherwise returns a blank hash. # # def options(*args) # args.extract_options! # end # # options(1, 2) # => {} # options(1, 2, :a …

短く書こう

railsのプラグイン書いててふと思いました。 def add_class_name(options, class_name) if options options.stringify_keys! if options['class'] options['class'] = "#{options['class']} #{class_name}" else options['class'] = class_name end options …

rhtml -> html.erb

手で一個ずつ変更してもいいんですけど、どこかで同じようなことやりそうだなーって思って、スクリプトを書きました。RAILS_ROOT/scriptにrhtml_to_html_erb.rbというファイル名で保存してください。使い方はこんな感じっす。 RAILS_ROOT> ruby script/rhtml…

ruby-debug

片平さんがまとめてくれた資料を見ながら、ruby-debugを使ってみようと思ってやってみました。 http://qwik.rubybizcommons.jp/197.html

capistranoのパスワード

cap deployなどを実行すると出てくるパスワードですが、実行するディレクトリのconfig/deploy.rb に set :password, "(パスワード)" と書けば入力せずにいけるんですね。これでサーバーの再起動とかも自動で行えるようになりますよね。 Subversionとかのリポ…

ruby/jruby 兼用config/database.yml

JRuby依存だったアプリのコードからほとんど依存部分を取り除けたので、jrubyから呼ばれたらjdbcmysqlを、rubyから呼ばれたらmysqlのアダプタを使うようにしたかったけど、それぞれ別のファイルとして用意して適宜書き換えしてたんだけど、面倒でいやんなっ…

rails2.0 と gettext

JRubyの方でもgettextを使ってうまくいくようになったし、DBをMySQLを使うようにしたのでCRubyでも動くはずーとか思って動かしてみたら、Internal Server Errorしか出ない。調べてみたら以下のエントリを発見。 rails2.0は多くのプラグインが動作しないとの…

form_for :builderオプション

そして、FormBuilderクラスを継承した独自の雛形を○○FormBuilderクラスとして定義しておき、それをform_forの:builderオプションで指定しておけば、独自の雛形を利用したフォームが生成されることになる。言葉で説明されてもピンとこないので、百聞は一見に…

暫定的な対応

とりあえずもともとのコードの意図を読み取った対応ではないので、全然お勧めできませんが、テスト動かなきゃしょうがないので、こんな対応をしました。 module ActiveRecord::ConnectionAdapters class JdbcAdapter def _execute(sql, name = nil) if JdbcC…

JdbcAdapterの怪

jruby-1.1RC1 + rails2.0.2でのこと。rake testをしてみたらフィクスチャのロードで以下のようなエラーが出ました。あ、ちなみにDBはHSQLDBね。activerecord-jdbchsqldb-adapter (0.7.1)使ってます。 1) Error: test_should_allow_signup(AccountControllerT…

labelヘルパーメソッド

ヘルパーメソッドにlabelっていうのがあるって今頃しりました。でさっそく使おうとか思ったんですけど、内部でhuman_attribute_nameではなくて、単にメソッド名をhumanizeしてるだけなのでgettext入れてても日本語化されないっす。マジでー?とりあえず、以…

Thin

先日、Mongrelの作者がエントリーしたRails Is A Ghettoはショッキングな内容だった。恐らくMongrelの開発スピードは停止または大幅に減速してしまうことだろう。 mongrelを利用しているらしいですが、便利さはどうなんでしょうね。是非試さなければ。

デバッガ

喜ばしいことに、Rails 2.0ではruby-debugを使ったdebuggerが正式に採用されました。 これの使い方は非常に簡単です。 ログ出力のみでデバッグと非常に辛い時があるので、今度使ってみようっと。

has_ancestor plugin

has_ancestor is a Ruby on Rails plug-in that provides an implementation of Class Table Inheritance based on Rails Polymorphic Associations. The plugin adds two methods to ActiveRecord::Base, has_ancestor and has_descendants. Railsが提供す…

Rails2.0.2のトラブルシューティング

If 0.9.5 is not the answer, do (you may not need to prefix with sudo, on OS X you do, some 窶蕨ix distributions too, but not Windows): sudo gem update --system これが重要

JRuby1.0.3

JRuby 1.0.3 Releasedされてました。 ためしてみたら、先日困った問題 attributes_with_quotes must have 2 arguments in Rails2.0! ってわけで、本来あるべき引数を足してみました。 これも解決してました。

JRuby on Rails2.0 jdbc_adapter

JRuby上で動かしていたRailsアプリを2.0に移行してみたら、 ArgumentError (wrong # of arguments(2 for 1)): C:/dev/jruby/jruby-1.0.2/lib/ruby/gems/1.8/gems/activerecord-2.0.1/lib/active_record/transactions.rb:129:in `rollback_active_record_stat…

RailsアプリをSubversionで管理する方法

This assumes that you have already created a subversion repository, and you are just wondering how to setup your rails project.If you have WEBrick or lighttpd running after running ???script/server??? in your rails directory, shutdown the …

Rails2.0 ビュー

ruby script/generate controllerでコントローラと一緒にビューを作ったら index.html.erbも作られました。試しにrhtmlも作ってみたら、ちゃんと動きました。共存してもOK。

Rails2.0 マイグレーション

def self.up create_table :events do |t| t.timestamps end endマイグレーションを生成してみたらこんなになっていた。tmiestampsってだいたい見当はつくけど、一応ActiveRecord::ConnectionAdapters::TableDefinitionを確認。 def timestamps column(:crea…

Rails2.0 rakeタスク

Rails 2.0 is finally finished after about a year in the making. 2.0が出たってことで色々試そうと思ってます。まずはざっくりrakeタスクの違いを見てみました。 rake db:abort_if_pending_migrations # Raises an error if there are pending migrations…

Rails2.0 config.breakpoint_server

******************************************************************* * config.breakpoint_server has been deprecated and has no effect. * *******************************************************************サーバーを起動すると、こんなメッセー…

Rails2.0 pagination

So how do I upgrade?If you want to move your application to Rails 2.0, you should first move it to Rails 1.2.6. That’ll include deprecation warnings for most everything we yanked out in 2.0. So if your application runs fine on 1.2.6 with n…

iPhone on Rails2

http://www.slashdotdash.net/articles/2007/12/04/iphone-on-rails-creating-an-iphone-optimised-version-of-your-rails-site-using-iui-and-rails-2Rails2でiPhone用の画面を作る話。

generatorのテンプレートでのローカル変数

generatorを作ってて、生成対象が存在しないときにはうまくいくのに、overwrite xxxx? と聞かれてyと答えると必ずエラーになる現象に遭遇。 undefined local variable or method `xxxxx' for #<Rails::Generator::Commands::Create:0x14b74a7>とかメッセージが出る。色々調べていたら、m.template の :assig</rails::generator::commands::create:0x14b74a7>…

Rails::Generator::NamedBaseのactions

railties/lib/rails_generator/generators/components/controller/controller_generator.rb#30 でactionsって書いてあるんだけど、代入しているところが見つからない。まさかと思って継承元を見るとrailties/lib/rails_generator/base.rb#200 に alias_meth…