20150625_SpeeeTeckParty

 20150625_SpeeeTeckParty

6/25に開催された、SpeeeTeckPartyで話したときに資料です。

865f591cd81499eb0512dd657f2f8316?s=128

Tatsuya Sasaki

June 30, 2015
Tweet

Transcript

  1. 3BJMT։ൃΛ͢Δ্Ͱ ஌͓͍ͬͯͨํ͕ྑͦ͞͏ͳ ̍̌ͷ͜ͱʢԾʣ 2015-06-25 @sasata299

  2. ࣗݾ঺հ w ͨͭ͞͞ w ώτϝσΟΞॴଐ w ͔Β͋͛ΤϯδχΞ w ٕज़ϒϩά

  3. ͔Β͋͛ ඒຯ͍͠

  4. <13>ຊΛग़͠·ͨ͠

  5. ͱ͍͏ࣄۀΛ΍͍ͬͯ·͢ɻ

  6. ᶃ)BTIOFX\cI LcI<L>\^^ ᶄ'BU.PEFM ᶅ$PODFSO ᶆϓϨθϯςʔγϣϯ૚ ᶇSBJMTDPOTPMFTBOECPY ᶈQMVDL ᶉpMUFS@QBSBNFUFST ᶊؔ࿈ͨ͠σʔλͷ࡟আ ᶋγϯάϧςʔϒϧܧঝʢ45*ʣ

    ᶌJODMVEFͱQSFQFOE
  7. ஌ͬͱ͘ͱྑͦ͞͏ᶃ Hash.new { |h,k| h[k] = {} }

  8. > hash = {} => {} > hash[:foo] => nil

  9. > hash = Hash.new { |h,k| h[k] = {} }

    => {} > hash[:foo] => {} # nil͡Όͳ͍ʂ
  10. > hash = Hash.new { |h,k| h[k] = {} }

    => {} > hash[:foo] => {} # nil͡Όͳ͍ʂ > hash[:foo][:bar] => nil > hash[:foo][:bar] = 1 => 1 > hash => { foo: { bar: 1 } }
  11. ஌ͬͱ͘ͱྑͦ͞͏ᶄ Fat Model

  12. Fat Controller Fat Model ??? ୭΋͕௨Δಓ

  13. Fat Controller Fat Model ??? ୭΋͕௨Δಓ ServiceΦϒδΣΫτ FormΦϒδΣΫτ, etc..

  14. ஌ͬͱ͘ͱྑͦ͞͏ᶅ Concern

  15. ؔ৺ࣄΛͻͱ͔ͨ·Γʹͯ͠؅ཧ͢Δʂ

  16. class Group before_save :set_default_name_if_need def post(comment); end end ! class

    Message before_save :set_default_name_if_need def post(comment); end end ಉ͡Α͏ͳॲཧ͕͋Δ৔߹
  17. module Commentable extend ActiveSupport::Concern ! included do before_save :set_default_name_if_need end

    ! def post(comment) Comment.create!(content: comment) end end
  18. class Group include Commentable end ! class Message include Commentable

    end εοΩϦ
  19. ஌ͬͱ͘ͱྑͦ͞͏ᶆ ϓϨθϯςʔγϣϯ૚

  20. ϑϧωʔϜΛද͍ࣔͨ͠ʂ

  21. module UserHelper def full_name(user) user.last_name + user.first_name end end VTFSΦϒδΣΫτΛ౉͢ͷΠέͯͳ͍

  22. class User < ActiveRecord::Base def full_name last_name + first_name end

    end 7JFXͰ͔͠࢖Θͳ͍ͷʹϞσϧʹஔ͘ʁ
  23. module UserDecorator def full_name last_name + first_name end end ྫ͑͹BDUJWF@EFDPSBUPSΛ࢖͏

  24. ஌ͬͱ͘ͱྑͦ͞͏ᶇ $ rails console —sandbox

  25. $ rails console —sandbox > user = User.first => #<User

    id: 1, name: "sasata299", like: "karaage"> > user.like = "sushi" => "sushi" > u.save! => true
  26. $ rails console —sandbox > user = User.first => #<User

    id: 1, name: "sasata299", like: "karaage"> > user.like = "sushi" => "sushi" > u.save! => true > user = User.first => #<User id: 1, name: "sasata299", like: "sushi">
  27. $ rails console > user = User.first => #<User id:

    1, name: "sasata299", like: “karaage"> ࠶౓DPOTPMFΛ։͘ͱSPMMCBDL͞ΕͯΔ
  28. $ rails console > user = User.first => #<User id:

    1, name: "sasata299", like: “karaage"> > _ => #<User id: 1, name: "sasata299", like: “karaage"> ͪͳΈʹΞϯμʔείΞศརͰ͢
  29. ஌ͬͱ͘ͱྑͦ͞͏ᶈ pluck

  30. ສ݅ͷ6TFSΛ༻ҙͯ͠JEͷҰཡΛऔಘ

  31. Benchmark.bm 10 do |r| r.report “select” do User.select(:id).map(&:id) end r.report

    “pluck” do User.pluck(:id) end end
  32. user system total real select 16.290000 0.760000 17.050000 ( 20.753481)

    pluck 4.350000 0.590000 4.940000 ( 6.712115) ഒҎ্଎͍ͧʂ
  33. User.select(:id) [#<User id:1>, #<User id: 2>, …] ΦϒδΣΫτΛੜ੒ NBQ JE

    ͢Δ [1, 2, …]
  34. User.pluck(:id) [1, 2, …] ActiveRecordΦϒδΣΫτͷ ੜ੒͕ൃੜ͠ͳ͍෼଎͍ʂ

  35. ஌ͬͱ͘ͱྑͦ͞͏ᶉ filter_parameters

  36. ͦͷ··ͩͱηΩϡΞͳ৘ใ͕ ϩάʹྲྀΕͯ͠·͏ʂʂʼʻ

  37. Started POST "/users" Processing by UsersController#create as HTML Parameters: {"email"=>"test@test.com",

    “password"=>"secret-value", “credit_card_number"=>"1111-2222-3333-4444"}
  38. 3BJMTBQQMJDBUJPODPOpHpMUFS@QBSBNFUFST  <QBTTXPSE DSFEJU@DBSE@OVNCFS> pMUFS@QBSBNFUFSTΛઃఆ͢Δ

  39. Started POST "/users" Processing by UsersController#create as HTML Parameters: {"email"=>"test@test.com",

    "password"=>"[FILTERED]", “credit_card_number"=>"[FILTERED]"}
  40. ஌ͬͱ͘ͱྑͦ͞͏ᶊ ؔ࿈ͨ͠σʔλͷ࡟আ

  41. class Blog < ActiveRecord::Base has_many :articles dependent: :destroy end #MPHΛ࡟আͨ͠Βؔ࿈͢Δ"SUJDMF΋࡟আ͢Δ

  42. EFMFUFͱEFTUSPZͷҧ͍ EFMFUF ୯७ʹ%&-&5&จ͕ൃߦ͞ΕΔ͚ͩ EFTUSPZ CFGPSF@EFTUSPZ΍BGUFS@EFTUSPZ΋࣮ߦ͞ΕΔ EFQFOEFOU΋ߟྀ͞ΕΔ

  43. ஌ͬͱ͘ͱྑͦ͞͏ᶋ γϯάϧςʔϒϧܧঝʢ45*ʣ

  44. ྑ͘ࣅͨϞσϧ͸ҰͭͷςʔϒϧͰѻ͓͏ ! 'PPEϞσϧͱ%SJOLϞσϧ͸ྑ͘ࣅͯΔ͔Β QSPEVDUTςʔϒϧͰ·ͱΊͯѻ͓͏ʂతͳ࿩

  45. class Product < ActiveRecord::Base # typeΧϥϜΛ͍࣋ͬͯΔ͜ͱʂ end ! class Food

    < Product; end class Drink < Product; end
  46. Food.create! <#Food id: 1, type: "Food", …> ! 2.times {

    Drink.create! } <#Drink id: 2, type: "Drink", …> <#Drink id: 3, type: "Drink", …>
  47. mysql> SELECT id, type FROM products; ! +----+-------+ | id

    | type | +----+-------+ | 1 | Food | | 2 | Drink | | 3 | Drink | +----+-------+
  48. Food.all [#<Food id: 1, type: "Food", …>] ! Drink.all [#<Drink

    id: 2, type: "Drink", …>, #<Drink id: 3, type: "Drink", …>] ! Product.all [#<Food id: 1, type: "Food", …>, #<Drink id: 2, type: "Drink", …>, #<Drink id: 3, type: "Drink", …>]
  49. ஌ͬͱ͘ͱྑͦ͞͏ᶌ include ͱ prepend

  50. ೚ҙͷϝιουΛੜ΍͍ͨ͠

  51. class Bar include Foo end ! p Bar.ancestors # [Bar,

    Foo, Object, …] #BSΫϥε͕༏ઌ͞ΕΔ
  52. class Bar prepend Foo end ! p Bar.ancestors # [Foo,

    Bar, Object, …] 'PPϞδϡʔϧ͕༏ઌ͞ΕΔ
  53. JODMVEFͱQSFQFOEͷڍಈͷҧ͍ JODMVEF ݩͷΫϥεͷޙΖʹ௥Ճ͞ΕΔ ಉ໊ͷϝιουͷ৔߹ɺݩͷΫϥε͕༏ઌ͞ΕΔ QSFQFOE ݩͷΫϥεΑΓલʹ௥Ճ͞ΕΔ ಉ໊ͷϝιουͷ৔߹ɺϞδϡʔϧ͕༏ઌ͞ΕΔ

  54. ᶃ)BTIOFX\cI LcI<L>\^^ ᶄ'BU.PEFM ᶅ$PODFSO ᶆϓϨθϯςʔγϣϯ૚ ᶇSBJMTDPOTPMFTBOECPY ᶈQMVDL ᶉpMUFS@QBSBNFUFST ᶊؔ࿈ͨ͠σʔλͷ࡟আ ᶋγϯάϧςʔϒϧܧঝʢ45*ʣ

    ᶌJODMVEFͱQSFQFOE
  55. ࠷ޙʹ

  56. https://www.wantedly.com/companies/hitomedia ΤϯδχΞืूͯ͠·͢ʂ

  57. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ @sasata299