Railsのrunnerについては The Rails Command Line の 1.6 rails runner に説明があります。
実行方法はrails runnderに続けて<モデルのクラス名>.<呼び出すメソッド>を指定します。
[bash]
$ rails runner "Model.long_running_method"
[/bash]
以前に自分でもまとめていました。
Rails3 runnerを利用してでバッチプログラムを実行するサンプル
日々のエンジニア的なお仕事を備忘録としてまとめています
Railsのrunnerについては The Rails Command Line の 1.6 rails runner に説明があります。
実行方法はrails runnderに続けて<モデルのクラス名>.<呼び出すメソッド>を指定します。
[bash]
$ rails runner "Model.long_running_method"
[/bash]
以前に自分でもまとめていました。
Rails3 runnerを利用してでバッチプログラムを実行するサンプル
readonly(false)を付ける
[ruby]
Post.joins(:comments).readonly(false)
[/ruby]
参照: ActiveRecordでINNER JOINするとsaveできなくなる(解決策あり)
form_helperで生成したselectは、オプションにdisabledを指定しても反映されません。
disabledにしたい場合には、HTMLオプションに指定します。
[ruby]
<%= f.select :content, options, {}, :disabled => true %>
[/ruby]
参考: RUBY ON RAILS – DISABLING A SELECT TAG USING THE SELECT HELPER
options_for_selectで、disabledを指定することもできますが、あまりこちらは使わないかも
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select
リソースのCRUDについては、Rails Guildの2.2 CRUD, Verbs, and Actionsにまとめられています。
よく使うところを抜粋してまとめてみます。
普通のリソース
resource :photos
HTTP Verb | Path | action | Named Helper |
---|---|---|---|
GET | /photos | index | photos_path |
GET | /photos/new | new | new_photo_path |
POST | /photos | create | photos_path |
GET | /photos/:id | show | edit_photo_patth(@photo) |
GET | /photos/:id/edit | edit | photo_patth(@photo) |
PUT | /photos/:id | update | photo_patth(@photo) |
DELETE | /photos/:id | destroy | photo_patth(@photo) |
resourcesに独自のパスを追加
resources: :posts do
comment "/comment", on: :collection
end
このときのNamed パスは
comment_posts_path
namespeceを使った場合
namespace :admin do resources :posts, :comments end
HTTP Verb | Path | action | named helper |
---|---|---|---|
GET | /admin/posts | index | admin_posts_path |
GET | /admin/posts/new | new | new_admin_post_path |
POST | /admin/posts | create | admin_posts_path |
GET | /admin/posts/:id | show | admin_post_path(:id) |
GET | /admin/posts/:id/edit | edit | edit_admin_post_path(:id) |
PUT | /admin/posts/:id | update | admin_post_path(:id) |
DELETE | /admin/posts/:id | destroy | admin_post_path(:id) |
Rails4
http://guides.rubyonrails.org/action_mailer_basics.html#example-action-mailer-configuration
Rails3.2
http://guides.rubyonrails.org/v3.2.13/action_mailer_basics.html#example-action-mailer-configuration
[ruby]
config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
[/ruby]
sendmailのテスト
[bash]
$ /usr/sbin/sendmail hoge@examle.com
Test mail
[/bash]
[Ctrl+D]で送信
Rails ActiveRecord – Batch Updates
http://apidock.com/rails/ActiveRecord/Base/update_all/class
Tag.where(:is_featured => true).each do |tag|
tag.update_attribute(:is_featured, false)
end
これだとTagオブジェクトがロードされるのでメモリーを食ってしまうので直接Update文を実行したい
Rails RDocのActiveRecord::Relation#update_allに記述されている書き方
Tag.where({:is_featured => true }).update_all({ :is_featured => false })
第二引数に条件を付けられるのでこちらの方が簡潔
Tag.update_all({ :is_featured => false }, {:is_featured => true })
どちらも実行されるSQLは同じ
UPDATE `tags` SET `is_featured` = 0 WHERE `tags`.`is_featured` = 1
公式ドキュメント
A Guide to The Rails Command Line 1.3 rails generate
Getting Up and Running Quickly with Scaffolding
Scafold
$ rails g scaffold Post title:string text:text invoke active_record create db/migrate/20130814020256_create_posts.rb create app/models/post.rb invoke test_unit create test/unit/post_test.rb create test/fixtures/posts.yml invoke resource_route route resources :posts invoke scaffold_controller create app/controllers/posts_controller.rb invoke erb create app/views/posts create app/views/posts/index.html.erb create app/views/posts/edit.html.erb create app/views/posts/show.html.erb create app/views/posts/new.html.erb create app/views/posts/_form.html.erb invoke test_unit create test/functional/posts_controller_test.rb invoke helper create app/helpers/posts_helper.rb invoke test_unit create test/unit/helpers/posts_helper_test.rb invoke assets invoke coffee create app/assets/javascripts/posts.js.coffee invoke scss create app/assets/stylesheets/posts.css.scss invoke scss create app/assets/stylesheets/scaffolds.css.scss
Controller
$ rails generate controller admin/Users
Model
Rspec