Ruby on Rails ステップアップ講座 - 大場寧子

  • 9,439 views
Uploaded on

11/26 (株)ウェブキャリア セミナー資料 …

11/26 (株)ウェブキャリア セミナー資料

DLはこちらからどうぞ
http://ko.meadowy.net/~nay/ruby/2007/seminar_ohba_2007_11_26-1.pdf

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • 使わないほうが良いということは別にないと思います。しかし、もちろん自動で結合されるもので足りていれば特に使う必要がないでしょう。経験上は、:joinsで細かい制御をする必要がある(そのほうが効率がよい)ケースもあると考えています。
    Are you sure you want to
    Your message goes here
  • コメントが何故か文字化けしてしまいました。どうしたら良いかわかりません。すみません。
    Are you sure you want to
    Your message goes here
  • プレゼン大変勉強になります。'RailsによるアジャイルWebアプリケーション 2nd ediion' (以下Rails本)を読みながらRailsを勉強しているのですが、このプレゼンを見ながら復習にふけっています。本当にありがとうございます。

    一点、気がついた点が有ったのでコメントさせてください。
    :joins (プレゼン P.69)についてなんですが、Rails本P.278に寄ると、「詳しくは18章で説明しますが、おそらくfindで:joinsパラメータを使うことはめったにありません。ほとんどの場合、一般的なテーブル間の結合は Active Record で自動的に処理されます。」とありました。

    :joins は、データベース依存の構文をコード内に埋め込む事になるので、使わない方が良いのかなと思いました。
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
9,439
On Slideshare
0
From Embeds
0
Number of Embeds
4

Actions

Shares
Downloads
178
Comments
3
Likes
10

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Rails 2007.11.26 y.ohba@everyleaf.com
  • 2. ... C++ Java(7 ) Ruby(1.5 ) Perl CGI, EJB, Servlet/JSP (Struts)
  • 3. orz
  • 4. Ruby on Rails Java Ruby on Rails
  • 5. Ruby on Rails (^0^)/
  • 6. Award on Rails 2006 • Web • • http://www.kozuchi.net
  • 7. Ruby on Rails 100% EC SNS
  • 8. Ruby on Rails http://www.everyleaf.com
  • 9. Award on rails 2007 BookScope http://bookscope.net/
  • 10. BookScope Amazon WebService .org
  • 11. Ruby on Rails /
  • 12. Ruby on Rails Ruby on Rails
  • 13. Hello, RoR Scaffold CRUD Session, Request
  • 14. Rails Ruby etc...
  • 15. API Rails , Plugin Ruby
  • 16. Hash Array REST ActiveRecord DRY Module Mix-in Ajax Plugin
  • 17. - IDE, SVN, Trac, Wiki, UML
  • 18. Ruby Java 0 ||= Hash Array
  • 19. Ruby - 0 s=0 if s p quot;0 quot; end nil false 0
  • 20. nil if value != nil ... if value ... unless value.nil? ...
  • 21. 1 if !id raise quot;no idquot; end Java raise quot;no idquot; if !id raise quot;no idquot; unless id unless
  • 22. Hash Array API
  • 23. Hash hash = {key => value, key => value, ...} value = hash[key] obj.foo( {key => value, key => value} ) obj.foo(key => value, key => value) {}
  • 24. Array admin= nil for element in @array if element.name == quot;adminquot; admin = element break end end admin = @array.detect{|e| e.name == quot;adminquot; }
  • 25. public, protected, private Java Module
  • 26. ||= name ||= quot; quot; name || name = quot; quot; ex) value += 1
  • 27. private, protected, public Ruby private Java Ruby protected Java Java Ruby public Java
  • 28. private item.my_private_method self.my_private_method private my_private_method private
  • 29. Ruby
  • 30. RoR - public - private protected ActiveRecord
  • 31. class Foo def self.foo p ‘foo’ end end > Foo.foo > f = Foo.new > f.class.foo
  • 32. ActiveRecord
  • 33. :joins :include
  • 34. Acts as List
  • 35. ActiveRecord O/R RDB ActiveRecord “ ”
  • 36. ActiveRecord table class users User user (id = 1, name = ‘ ’)
  • 37. class table class class
  • 38. (1) Item, Book, Dvd items Item items Book Dvd
  • 39. Item.find(:all, :order => ‘created_at desc’) DVD, Book.find(:all, :order => ‘created_at desc’)
  • 40. type
  • 41. type items Item type: varchar(30) quot;Bookquot; Book Dvd type
  • 42. (2) Manager Developer ProjectMember project_members class table class class Developer Manager
  • 43. item_type role if
  • 44. Polymorphic Association
  • 45. (Association) = (= ) belongs_to has_one has_many
  • 46. belongs_to Employee Office offices employees office_id belongs_to :office class Employee < ActiveRecord::Base belongs_to :office end belongs_to( )
  • 47. Controller Employee @employee = Employee.find(params[:id]) View <table> <tr> <th> </th> <td><%= @employee.office.name %></td> </tr> </table>
  • 48. has_one has_one :head Employee Office offices employees office_id class Office < ActiveRecord::Base has_one :head, :class_name => ‘Employee’, :conditions => “head = 1” end has_xxx ( )
  • 49. has_many has_many :employees Employee Office offices employees office_id class Office < ActiveRecord::Base has_many :employees end has_one has_many
  • 50. has_many Controller office @office = Office.find(params[:id]) View <% for e in @office.employees -%> <div><%= e.name %></div> <% end ->
  • 51. Office offices Employee employees Factory factories Employee Office Factory
  • 52. ... class Employee < ActiveRecord::Base belongs_to :office belongs_to :factory end <% if @employee.office %> <%= @employee.office.name %> <% else -%> <%= @employee.factory.name %> <% end -%>
  • 53. class Employee < ActiveRecord::Base belongs_to :workplace end Office Factory WorkpPlace <%= @employee.workplace.name %>
  • 54. has_many :employees has_one :head Employee Office offices employees office_id belongs_to :office
  • 55. has_many :employees, :as => work_place Office offices work_place_id work_place_type Work Employee employees Place Factory factories belongs_to :work_place, :polymorphic => true
  • 56. (xxx_type )
  • 57. class Employee < ActiveRecord::Base belongs_to :work_place, :polymorphic => true end class Office < ActiveRecord::Base has_many :employees, :as => :work_place end class Factory < ActiveRecord::Base has_many :employees, :as => :work_place end
  • 58. work_place_id, work_place_type xxx_id, xxx_type xxx xxx_type SQL conditions ActiveRecord
  • 59. Comment comments commentable_id commentable_type
  • 60. class Item < ActiveRecord::Base has_many :comments, :as => :commentable end @item.comments.create(:body => ‘ ’)
  • 61. (xxx_)type Up
  • 62. ActiveRecord Save
  • 63. # def after_update children.each {|c| c.save!} end
  • 64. before_validation after_destroy
  • 65. - Controller Model Rails API
  • 66. :joins :include employees = Employee.find(:all, :select => quot;employee.*quot;, :conditions => [quot;offices.prefectue_id = ?quot;, prefecture_id], :joins => quot;left join offices on employees.office_id = offices.idquot;) :joins SQL join
  • 67. :joins :include (2) DB class Employee < ActiveRecord::Base belongs_to :office end employee = Employee.find(@id, :include => :office)
  • 68. API validates_presence_of etc... = errors def validate errors.add_to_base(“ ”) unless quantity % 2 == 0 end
  • 69. if @employee.save flash[:notice] = ‘ ’ redirect_to :action => ‘index’ else render :action => ‘new’ end Controller save <%= error_messages_for ‘employee’ %> View
  • 70. API API ActiveHeart Plugin GetText , Scpecial Generator
  • 71. Acts as List has_many
  • 72. Acts as List position class Note < ActiveRecord::Base has_many :lines, :order => quot;positionquot; end class Line < ActiveRecord::Base rails 2.0 belongs_to :note acts_as_list :scope => :note end position
  • 73. has_many )
  • 74. class Employee < ActiveRecord::Base belongs_to :office, :counter_cache => true end offices employees_count 0
  • 75. ActiveRecord API Association (has_many ) http://api.rubyonrails.org/
  • 76. REST Controller URL entries/new_comment/1 entries/1/comments/new entries/1/comments/3
  • 77. REST Controller routes.rb Filter @book
  • 78. rails 2.0 REST
  • 79. acts_as_authenticated plugin before_filter before_filter :load_user private def load_user @user = User.find(session[:user_id]) end
  • 80. @entries = Entry.secure_find(@user, options)
  • 81. create save update validates_association :dependent => :destroy
  • 82. acts_as_paranoid
  • 83. file_column RMagick I/F
  • 84. GetText (gem ) Globalize
  • 85. acts_as_taggable SpecialGenerator Scaffold
  • 86. Rails
  • 87. Ruby DSL DSL = has_many :employees AR Rails = DSL
  • 88. String under_score, pluralize ... acts_as_paranoid
  • 89. Module Mix-in
  • 90. Java Ruby class Object def yeah p ‘yeah!’ end end
  • 91. Module Mix-in Module module Human def age(date) # () end end include class Employee < ActiveRecord::Base include Human end
  • 92. ActiveRecord::Base Module Office Factory include include WorkPlace
  • 93. include module WorkPlace # def self.included(base) base.has_many :employees, :as => :work_place end end
  • 94. Module module WorkPlace module ClassMethods def foo p ‘foo’ end end def self.included(base) base.extend(ClassMethods) end end
  • 95. private obj.__send__(:himitsu) Ruby1.9
  • 96. clazz.__send__(:define_method, :yeah) { p ‘yeah!’ }
  • 97. (^o^)/
  • 98. init.rb Module include DSL ) acts_as_paranoid
  • 99. NULL email def email=(value) value = nil if value.blank? self[:email] = value end
  • 100. class User < ActiveRecord::Base nullify_blank :email, :address, :tel, :fax, :url end
  • 101. vendor/plugins/my_extension/init.rb class ActiveRecord::Base def self.nullify_blank(*args) args.each do |attr| define_method(:quot;#{attr.to_s}=quot;) do |v| v = nil if v.blank? self[attr.to_sym] = v end end end end
  • 102. Module Helper DSL