Ruby on Rails 中級者を目指して - 大場寧子

14,065 views

Published on

(株)ウェブキャリア 2007/10/18 のセミナー資料

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

Published in: Technology
1 Comment
27 Likes
Statistics
Notes
No Downloads
Views
Total views
14,065
On SlideShare
0
From Embeds
0
Number of Embeds
166
Actions
Shares
0
Downloads
565
Comments
1
Likes
27
Embeds 0
No embeds

No notes for slide

Ruby on Rails 中級者を目指して - 大場寧子

  1. 1. Ruby on Rails y.ohba@everyleaf.com
  2. 2. ... → C++ → Java(7 ) → Ruby(1.5 ) Award on Rails 2006
  3. 3. orz
  4. 4. Award on Rails 2006 • Web • • http://www.kozuchi.net
  5. 5. Ruby on Rails 100% EC SNS
  6. 6. Ruby on Rails http://www.everyleaf.com
  7. 7. Ruby on Rails /
  8. 8. Ruby on Rails Java
  9. 9. Hello, RoR Scaffold CRUD Session, Request
  10. 10. Rails Ruby etc...
  11. 11. API Rails , Plugin Ruby
  12. 12. REST Hash Array ActiveRecord Module Mix-in DRY Ajax Plugin
  13. 13. - IDE, SVN, Trac, Wiki, UML
  14. 14. Java Ruby 0 ||= Hash Array
  15. 15. Ruby - 0 s=0 if s p "0 " end nil false 0
  16. 16. nil if value != nil ... if value ... unless value.nil? ...
  17. 17. 1 if !id raise "no id" end Java raise "no id" if !id raise "no id" unless id unless
  18. 18. Hash Array API
  19. 19. Hash hash = {key => value, key => value, ...} value = hash[key] obj.foo( {key => value, key => value} ) obj.foo(key => value, key => value) {}
  20. 20. Array admin= nil for element in @array if element.name == "admin" admin = element break end end admin = @array.detect{|e| e.name == "admin" }
  21. 21. public, protected, private Java Module
  22. 22. ||= name ||= " " name = name || " " ex) value += 1
  23. 23. private, protected, public Ruby private Java Ruby protected Java Java public Ruby 
 Java
  24. 24. Ruby Java
  25. 25. item.save item self.save self save private
  26. 26. RoR - public - private protected ActiveRecord
  27. 27. class Foo def self.foo p ‘foo’ end end > Foo.foo > f = Foo.new > f.class.foo
  28. 28. ActiveRecord
  29. 29. :joins :include
  30. 30. Acts as List
  31. 31. class table class class
  32. 32. Item, Book, Dvd items Item items Book Dvd
  33. 33. type
  34. 34. type items Item type: varchar(30) "Book" Book Dvd type
  35. 35. Employee belongs_to :work_place
  36. 36. has_many :employees has_one :employee employees Employee Office offices office_id belongs_to :office
  37. 37. has_many :employees, :as => work_place Office offices work_place_id work_place_type employe Work es Employee Place Factory factories belongs_to :work_place, :polymorphic => true
  38. 38. work_place_id, work_place_type xxx_id, xxx_type xxx id type( ) xxx_type has_many :through
  39. 39. (xxx_)type → Up
  40. 40. ActiveRecord Save
  41. 41. # def after_update children.each {|c| c.save!} end
  42. 42. - Controller Model Rails API
  43. 43. :joins :include employees = Employee.find(:all, :select => "employee.*", :conditions => ["offices.prefectue_id = ?", prefecture_id], :joins => "left join offices on employees.office_id = offices.id") :joins SQL join
  44. 44. :joins :include (2) class Employee < ActiveRecord::Base belongs_to :office end employee = Employee.find(@id, :include => :office)
  45. 45. Rails API validates_presence_of etc... = errors def validate errors.add_to_base(“ ”) unless quantity % 2 == 0 end
  46. 46. API :scope, :allow_nil ActiveHeart Plugin GetText , Scpecial Generator
  47. 47. Acts as List has_many
  48. 48. Acts as List position class Note < ActiveRecord::Base has_many :lines, :order => "position" end class Line < ActiveRecord::Base belongs_to :note rails 2.0 acts_as_list :scope => :note end
  49. 49. has_many )
  50. 50. class Employee < ActiveRecord::Base belongs_to :office, :counter_cache => true end ★offices employees_count ★ 0
  51. 51. ActiveRecord API Association (has_many ) http://api.rubyonrails.org/
  52. 52. REST Controller URL entries/new_comment/1 entries/1/comments/new entries/1/comments/3
  53. 53. REST Controller routes.rb Filter @book
  54. 54. rails 2.0 REST
  55. 55. acts_as_authenticated plugin before_filter before_filter :load_user private def load_user @user = User.find(session[:user_id]) end
  56. 56. @entries = Entry.secure_find(@user, options)
  57. 57. create save update validates_association :dependent => :destroy
  58. 58. acts_as_paranoid
  59. 59. file_column RMagick I/F
  60. 60. GetText (gem ) Globalize
  61. 61. acts_as_taggable SpecialGenerator Scaffold
  62. 62. Rails
  63. 63. Ruby DSL DSL = has_many :employees AR Rails = DSL
  64. 64. String under_score, pluralize ... acts_as_paranoid
  65. 65. Module Mix-in
  66. 66. Java Ruby class Object def yeah p ‘yeah!’ end end
  67. 67. Module Mix-in Module module Human def age(date) # ( ) end end include class Employee < ActiveRecord::Base include Human end
  68. 68. / ActiveRecord::Base Office Factory Module include include WorkPlace
  69. 69. include module WorkPlace # def self.included(base) base.has_many :employees, :as => :work_place end end
  70. 70. Module module WorkPlace module ClassMethods def foo p ‘foo’ end end def self.included(base) base.extend(ClassMethods) end end
  71. 71. private obj.__send__(:himitsu) ※Ruby1.9
  72. 72. clazz.__send__(:define_method, :yeah) { p ‘yeah!’ }
  73. 73. (^o^)/
  74. 74. init.rb Module include → DSL ) acts_as_paranoid
  75. 75. Module Helper DSL
  76. 76. What
  77. 77. (^o^)/
  78. 78. Ruby on Rails
  79. 79. Award on rails 2007 BookScope http://ko.meadowy.net/bookscope/
  80. 80. BookScope Amazon WebService .org
  81. 81. 10/19( ) !! http://rails.drecom.jp/vote BookScope m(_ _)m

×