ðŸ Railséçºè ãæ¡çšé¢æ¥ã§èãããæ³å®Q&A 53åïŒç¿»èš³ïŒãèªåãªãã«æ·±å ãããŠã¿ã
åŒçšå
ðŸ Q1: ããã°ã¢ããªã§èšäºã®ãªã¹ããååŸãããšãã®ãªã¯ãšã¹ã/ã¬ã¹ãã³ã¹ãµã€ã¯ã«ãã²ãšãšãã説æããŠãã ãã
- ãŠãŒã¶ãŒããã¿ã³ãã¯ãªãã¯ãã
/articles
ãšããURLãžGETãªã¯ãšã¹ããWebãµãŒããŒããRails
ã«æž¡ããã-
Rails
ã§ã¯ãroutes.rb
ã«å²ãåœãŠãããŠããURL/ã³ã³ãããŒã©ã«åºã¥ããŠããªã¯ãšã¹ãã«çŽã¥ãã³ã³ãããŒã©ã¢ã¯ã·ã§ã³ãå®è¡ããŸãïŒããã§ã¯index
ã¢ã¯ã·ã§ã³ïŒ -
Article
ã³ã³ãããŒã©ã®index
ã¢ã¯ã·ã§ã³ã§ã¯ãArticle.all
ã§Article
ã¢ãã«çµç±ã§ããŒã¿ããŒã¹ããããŒã¿ãèªã¿èŸŒã¿ãã€ã³ã¹ã¿ã³ã¹å€æ°ã«ä»£å ¥ãããŸã - ãã¥ãŒã§ã€ã³ã¹ã¿ã³ã¹å€æ°ãåãåããç¹°ãè¿ãåŠçãªã©ã§å€ãå±éãããªã¯ãšã¹ããéä¿¡ããŠãŠãŒã¶ãŒã«ã¬ã³ããªã³ã°ããŸã
issue01
ããå解ããŠã¿ãŠãé¢çœãã
ðŸ Q2:ãRubyã§ã¯ïŒã»ãŒïŒãããããã®ããªããžã§ã¯ãã§ãããã«ã€ããŠèª¬æããŠãã ãã
issue02
Rubyãšä»ã®ãªããžã§ã¯ãæåèšèªã®éãã£ãŠãªã«ãããã?
ã¯ã©ã¹ã䜿çšããªãå Žåã®æ¯ã¹ãŠãããŒã¿ãšã¡ãœããã管çãããããªããããŒã¿ãã¯ã©ã¹ãäžç·ã«æã¡éã¹ãã®ãã¯ã©ã¹ã®ã¡ãªããã
åŒçšïŒãã§ãªãŒæ¬
ðŸ Q3: Rubyã®åã¯éçã§ããïŒåçã§ããïŒ
Rubyã¯åçåä»ãèšèª ïŒå€æ°ãªã©ã®ããŒã¿åã®å®£èšããããªãããã°ã©ãã³ã°èšèªïŒ
åçåä»ãã®ã¡ãªãã
- èšè¿°éãæžã
- æ¯èŒçç°¡åã«ããã°ã©ã ãæžãã
åèæç®
ðŸ Q4: Rubyã®ã²ãã¿ãŒãšã»ãã¿ãŒã«ã€ããŠèª¬æããŠãã ãã
ã²ãã¿ãŒãšã¯ãã²ãã¿ãŒãçšããŠã€ã³ã¹ã¿ã³ã¹å€æ°ã«ã¢ã¯ã»ã¹ã§ããããã«ãªã
ã»ãã¿ãŒãšã¯ãã»ãã¿ãŒãçšããŠã€ã³ã¹ã¿ã³ã¹å€æ°ã«å€ãèšå®ã§ãã
# ã»ãŒäœ¿çšããªããã©ãæåã§å®çŸ©ãããšä»¥äžã®ããã«ãªã
class Car
# ã²ãã¿ãŒã®å®çŸ©
def color
@color
end
# ã»ãã¿ãŒã®å®çŸ©
def color=(color)
@color = color
end
end
c = Car.new
c.color = 'red'
puts c.color # => red
# attr_accessorã䜿çšããå Žå
class Car
attr_accessor :color
end
c = Car.new
c.color = 'blue'
puts c.color #=> blue
ðŸ Q5: Rubyã§ããã¡ãœãããåŒã³åºãããšãã®åäœã説æããŠãã ãã
ãªããžã§ã¯ãã«å¯ŸããŠã¡ãœãããåŒã³åºããŸã
ã¡ãœããã¯ã¯ã©ã¹å ã§å®çŸ©ããããã®ã¯ã©ã¹ããäœæããããªããžã§ã¯ã(ã¬ã·ãŒããŒãšåŒã°ããŸã)ã«å¯ŸããŠè¡ããããåŠçãèšè¿°ãããã®ã§ãããã®çºãã¡ãœãããåŒã³åºãæã«ã¯å¯Ÿè±¡ãšãªããªããžã§ã¯ããšå®è¡ãããã¡ãœããåãæå®ããŠåŒã³åºããŸãã
åŒçš : Let'sããã°ã©ãã³ã°
ðŸ Q6: ããRailsã¢ããªå ã®ã«ãŒãã£ã³ã°ããã¹ãŠè¡šç€ºããŠãã ãã
$ rails routes
ðŸ Q7: Gemfileã«ã€ããŠèª¬æããŠãã ãã
Gemfileã¯ãã¢ããªã±ãŒã·ã§ã³ã§å©çšãããäŸåé¢ä¿ãæå®ããŸãã
ã«ãŒããã£ã¬ã¯ããªã«çœ®ããã
ðŸ Q8: Gemfile.lockã«ã€ããŠèª¬æããŠãã ãã
Gemfile.lockã«ã¯ãã€ã³ã¹ããŒã«ãããgemã®æ£ç¢ºãªããŒãžã§ã³ãèšé²ãããŠããŸã
ðŸ Q9: Railsã§ã©ããªãã¶ã€ã³ãã¿ãŒã³ã䜿ã£ãããšããããŸããïŒ
- Service Objectãã¿ãŒã³
è¥å€§åããActiveRecordã¢ãã«ãåå²ããã³ã³ãããŒã©ãã¹ãªã ãã€èªã¿ãããããããã§éåžžã«æçšãªãRuby on Railséçºã«ãããäžçš®ã®éçºãã¿ãŒã³
â ãrailsã«ãã
- Validatorãã¿ãŒã³
ActiveRecord::Baseç¶æ¿ã¯ã©ã¹ã®ã¬ã³ãŒããæ€èšŒãã責å
- Policy Objectãã¿ãŒã³
ããžãã¹ã«ãŒã«ãã«ãã»ã«åãã責åïŒpunditïŒ
- Decoratorãã¿ãŒã³
ã¢ãã«ã«å¯Ÿãããã¥ãŒã®ããžãã¯ãã«ãã»ã«åãã責å
䜿ã£ãããšã¯ãªããã©ãéèŠ
- Query Objectãã¿ãŒã³
ActiveRecord::Relationã«å¯ŸããŠçµåãçµã蟌ã¿ããœãŒããªã©ã®æäœãè¡ããRelationãè¿ã責å
- Form Objectãã¿ãŒã³
ãŠãŒã¶ããã®å ¥åãæŽåœ¢ã»æ€èšŒããŠæ°žç¶åãã責å
issue03
ã³ãŒããªãŒãã£ã³ã°ãã
ðŸ Q10: Railsã§ã¯ããŒã¿ããŒã¹ã®ã¹ããŒããã©ã®ããã«ç®¡çããã説æããŠãã ãã
æåã§ãã€ã°ã¬ãŒã·ã§ã³ãã¡ã€ã«ãçæããŠãæ瀺ãè¿œå ãã
ãã€ã°ã¬ãŒã·ã§ã³ãã¡ã€ã«ã¯ã Active Recordã«å¯ŸããŠæ¢åã®ããŒã¿ããŒã¹ã®ã¹ããŒãã®ãå€æŽæ¹æ³ããæ瀺ããã ãã®ããã«ãéå»ã®ãã€ã°ã¬ãŒã·ã§ã³ãã¡ã€ã«ãåé€ãŸãã¯å€æŽãããšããŒã¿ããŒã¹ã®ã¹ããŒãã«æªåœ±é¿ãåãŒãå¯èœæ§ãããã
issue04
æ¥æ¬èªããããã«ããããå€æŽãã
ðŸ Q11: countãšlengthãšsizeã®éãã説æããŠãã ãã
count
: ç¹å®ã®æåæ°ãèŠçŽ ãã«ãŠã³ãããããšãã®ã¿countã䜿çšããã
count
ã¯EnumerableïŒã€ãã¥ãŒãã©ãã«ïŒã¢ãžã¥ãŒã«ã§å®çŸ©ãããŠããã¡ãœããã§ãå®éã«eachåŠçãåããŠã«ãŠã³ãããçµæãåºåããŠãã
length
: å
šã«ãŠã³ãã®å Žåã¯ãåŠçé床ãæ©ãã
size
ã¯length
ã®ãšã€ãªã¢ã¹
size
,Âlength
 ã¯ã¬ã·ãŒãããã§ã«æã£ãŠããèŠçŽ æ°æ å ±ãåç §ãã
ïŒããŒã¿ããŒã¹ãã©ã³ã¶ã¯ã·ã§ã³ãå®è¡ããªãããcountããæ©ãïŒ
ðŸ Q12: èªå¯ïŒauthorizationïŒãã©ã®ããã«å®è£ ããŸãããïŒ
cancancan
ã§å®è£
ðŸ Q13: ã³ãŒã«ããã¯ãšã¯äœãã説æããŠãã ãã
ã³ãŒã«ããã¯ã¯ããªããžã§ã¯ãã®ã©ã€ããµã€ã¯ã«ã®äžã§ã¡ãœãããå®è¡ãããã㯠ãæã
ðŸ Q14: before_saveã³ãŒã«ããã¯ãšafter_saveã³ãŒã«ããã¯ã®äœ¿ãåãã«ã€ããŠèª¬æããŠãã ãã
before_save
ããªããŒã·ã§ã³ã«æåããå®éã«ãªããžã§ã¯ããä¿åãããçŽåã§å®è¡ããã
ãããªããžã§ã¯ãã®å±æ§ãæŽæ°ããå Žåã¯
before_save
ããã
after_save
: ããŒã¿ããŒã¹ãžã®COMMIT
ã®çŽåã«å®è¡ããã
ãããªããžã§ã¯ãã«é¢é£ãªããžã§ã¯ããæäœãããšãã«äœ¿çšãã
ðŸ Q15: Railsã®ãã€ãã·ã£ã©ã€ã¶ãã«ã€ããŠèª¬æããŠãã ãã
ã€ãã·ã£ã©ã€ã¶ã«ã¯ãã¢ããªã®èµ·åæã«ã®ã¿å®è¡ããèšå®ããžãã¯ïŒãã¡ã€ã«ãªã©ïŒ ã眮ããŸã
ðŸ Q16: deleteãšdestroyã®éãã説æããŠãã ãã
delete : ã¬ã³ãŒãã1件åé€ãã
destroy : ã¬ã³ãŒãã1件åé€ããŠãã³ãŒã«ããã¯ãå®è¡ãã
ðŸ Q17:ããã¡ããã¢ãã«ãèãã³ã³ãããŒã©ãã®æå³ã説æããŠãã ãã
ããžãã¹ããžãã¯ã¯ã³ã³ãããŒã©ã§ã¯ãªããªãã¹ãã¢ãã«ã«é
眮ãã¹ã
ããããããšã§ããžãã¯ã®åäœãã¹ããè¡ãããããªããåå©çšæ§ãåäžãã
ã³ã³ãããŒã©ã¯ããã¥ãŒãšã¢ãã«ã®éã§æ
å ±ãåãæž¡ãããããã®å Žã§ãããªã
ðŸ Q18:ãèãã³ã³ãããŒã©ãèãã¢ãã«ãã®æå³ã説æããŠãã ãã
ã³ãŒãéãå€ããªãã«ã€ããŠããã¡ããã¢ãã«ãæã«è² ããªããªã
ã¢ãã«ã管çäžèœ
ã«é¥ã£ãŠããŸã
ã¢ãã«å
ã®ããžãã¯ãè¥å€§åãããªãããã«ããã¹ã
ðŸ Q19: ã¯ã©ã¹ã¡ãœãããšã€ã³ã¹ã¿ã³ã¹ã¡ãœããã®éãã説æããŠãã ãã
ã¯ã©ã¹ã¡ãœãã : ã¯ã©ã¹äžã§å©çšã§ãã
ã€ã³ã¹ã¿ã³ã¹ã¡ãœãã : ã€ã³ã¹ã¿ã³ã¹äžã§å©çšã§ãã
ã¯ã©ã¹ã«å±ããã¡ãœããïŒã¯ã©ã¹ã¡ãœããã¯Personèªèº«ã«é¢ããæ å ±ã®
å€æŽãåç §ã®åœ¹å²ããã£ãŠããŸããä»åã ãš Person.maleãPerson.femaleãPerson.sizeã§ãã
ã€ã³ã¹ã¿ã³ã¹ã«å±ããã¡ãœããã¯ãåå¥ã®ã€ã³ã¹ã¿ã³ã¹ã«é¢ããæ å ±ã®
å€æŽãåç §ã®åœ¹å²ããæã£ãŠããŸãã
ä»åã ãš Person#helloã§ãã
以äžQiitaèšäºããåŒçš
ðŸ Q20: POROã«ã€ããŠèª¬æããŠãã ãã
POROïŒPlain Old Ruby ObjectïŒActiveRecordãªã©ãç¶æ¿ããŠããªãçŽ ã®Rubyãªããžã§ã¯ãã®ããš
æ£çŽãã€äœ¿çšãããããããããªãããã©ã³ã¶ã¯ã·ã§ã³ã¿ãããªåŠçãããæã«äœ¿çšããã¿ããã ãã©
ãã®èŸºèªãã§èŠã
ðŸ Q21: Rubyã§å€éç¶æ¿ã¯äœ¿ããŸããïŒ
ãã®ä»£ãããincludeãextend ã«ããã¢ãžã¥ãŒã«ã®ããã¯ã¹ã€ã³ ãå©çšã§ãã
include
ã¯ã€ã³ã¹ã¿ã³ã¹ã¡ãœãããšããŠ
extend
ã¯ã¯ã©ã¹ã¡ãœãããšããŠåŒã³åºãããã¯ã©ã¹ã«ã¢ãžã¥ãŒã«ãincludeããŠæ©èœãè¿œå ããããšãããã¯ã¹ã€ã³ãšãã
ðŸ Q22: Rubyã¯ã匷ãåä»ããã匱ãåä»ããã®ã©ã¡ãã§ããïŒ
Ruby
ã¯åŒ·ãåä»ã
匱ãåä»ãã¯PHP
ãJS
ãªã©
# 匷ãåä»ãã®å Žå
"hello" + 1
#=> ãšã©ãŒ
# 匱ãåä»ãã®å Žå
"hello" + 1
#=> "hello1"
ðŸ Q23: ããã¯ã°ã©ãŠã³ããžã§ãã«ã©ããªãã¬ãŒã ã¯ãŒã¯ã䜿ã£ãããšããããŸããïŒ
éãåŠçãªã©ãããã°ã°ã©ãŠã³ãã§å®è¡ããããã®ãªã©ãããæã«äœ¿çšã§ãã
ïŒè£ã§å®è¡ããŠãå®éã¯ä»ã®æäœãã§ãããªã©ïŒ
ãã¥ãŒã€ã³ã°ãšã¯ã ã¢ããªã±ãŒã·ã§ã³ãã¡ãã»ãŒãžã®åŠçãè¡ããããã«ãªããŸã§ã¡ãã»ãŒãžãä¿çããæ©èœã®ããšã ãã¥ãŒã€ã³ã°ã«ãã£ãŠæ¬¡ã®ããšãå¯èœã«ãªãã éä¿¡ã³ãŒããèšè¿°ããããšãªããããã°ã©ã éã§éä¿¡ãè¡ãããšãã§ãã (ãããã®ããã°ã©ã ã¯ããããç°ãªã£ãç°å¢ã§å®è¡äžã§ãæ§ããªã)
issue05
ãã³ãºãªã³ã§äœ¿çšããŠã¿ãïŒãRails
ïŒ
ðŸ Q24: Rubyã®ã¯ã©ã¹ã§ã³ã³ã¹ãã©ã¯ã¿ã宣èšããæ¹æ³ã説æããŠãã ãã
ã³ã³ã¹ãã©ã¯ã¿ã¯initialize
ã¡ãœããã§å®çŸ©ãã
ãã®ã¡ãœãã㯠ã¯ã©ã¹ã®æ°ããã€ã³ã¹ã¿ã³ã¹ãåæåããããšãã«åŒã³åºãããŸã
ðŸ Q25: ãã«ããŒã«ã¯ã©ã®ãããªããžãã¯ã眮ããŸããïŒ
ãã«ããŒã®ããžãã¯ã¯ããã¥ãŒã§äœ¿çšãããã®
ðŸ Q26: Active Recordã«ã€ããŠèª¬æããŠãã ãã
Active Recordã¯ãã¢ãã«ãšããŒã¿ããŒã¹ã«å¯Ÿå¿ã€ããORM(Object-Relational Mapping)ã®ããš
Active Recordã®SQLã€ã³ãžã§ã¯ã·ã§ã³ã®ä¿è·æ©èœ
ããã·ã¥ã§èšè¿°ãããšä¿è·ã«ãªããçŽæ¥ä»£å ¥ãããšãŸããã
ðŸ Q27: Rubyã®selfã¯ã©ããªãšãã«äœ¿ããã説æããŠãã ãã
- ã¯ã©ã¹ã¡ãœãããåŒã¶ãšã
- ã€ã³ã¹ã¿ã³ã¹ããã¯ã©ã¹ã¡ãœãããåŒã³åºãå Žåã¯
self.class.method
ãšããåŒã³åºãæ¹æ³ãå¿ é ãšãªã
- ã€ã³ã¹ã¿ã³ã¹ããã¯ã©ã¹ã¡ãœãããåŒã³åºãå Žåã¯
- ã¯ã©ã¹å
ã§ã¯ã
self
ãçšããŠçŸåšã®ã¯ã©ã¹ãåç §ãã
issue06
åèª
ðŸ Q28: Rackã«ã€ããŠèª¬æããŠãã ãã
Rackãšã¯ãã¢ããªã±ãŒã·ã§ã³ãµãŒããŒãšãã¬ãŒã ã¯ãŒã¯ã®éã«äœçœ®ããAPI
issue07
Zennã«äœç³»çã«ãŸãšããŠãããããïŒãRails
ã«è©³ããèšè¿°ããïŒ
ðŸ Q29: MVCã«ã€ããŠèª¬æããŠãã ãã
MVCïŒModel-View-ControllerïŒã¯ Railsãæ§ç¯ãããœãããŠã§ã¢ãã¶ã€ã³ãã¿ãŒã³
ðŸ Q30: Rubyã®ãããã¯ã«ã€ããŠèª¬æããŠãã ãã
Rubyã®ãããã¯ã¯ãã³ãŒããæ³¢æ¬åŒ§{ }
ãŸãã¯doãšend
ã§å²ãã ãã®ã§ã
eachãåŒã³åºããšãã«ã¯ãããã¯ãã²ãšã€æž¡ããŸã
ðŸ Q31: procãšlambdaã®éãã説æããŠãã ãã
äžèšãèžãŸããŠéã
- åŒæ°ãã§ãã¯
- returnã®æå
# Proc.newãšlambdaã®éã
éãâ ïŒåŒæ°ãã§ãã¯ïŒ
# Proc.new
proc1 = proc { |arg| p arg }
proc1.call( 'proc', 'lambda') #=> "proc"
"Proc.newã®å Žåã¯æž¡ãåŒæ°ãå€ããšãå
é ããå¿
èŠãªæ°ã ãåã£ãŠåŸã¯ç¡èŠã"
"å°ãªããšè¶³ããªãéšåã« nil ãå²ãåœãŠãããå€é代å
¥ã«è¿ãæ±ãæ¹ãããŸã"
# lambda
lamd = lambda { |arg| p arg }
lamd.call('lambda') #=> "lambda"
lamd.call('lambda', 'proc') #=> wrong number of arguments (given 2, expected 1) (ArgumentError)
"åŒæ°ãéããšãšã©ãŒã«ãªã"
éãâ¡ïŒreturnã®æåïŒ
# Proc.new
def proc_method
proc = Proc.new { return p "proc"}
proc.call
p "proc method"
end
proc_method #=> "proc"
"Proc.newã®å Žåã¯returnåŸã«ã¡ãœããèªäœãæããã"
# lambdaïŒã©ã ãïŒ
def lambda_method
lambda1 = lambda { return p "lambda"}
lambda1.call
p "lambda method"
end
lambda_method #=> "lambda"
#=> "lambda method"
"lambdaã®å Žåã¯returnããåŸã«ã¡ãœããã«æ»ããã¡ãœãããæåŸãŸã§å®è¡ããŸãã"
issue08
ã³ãŒããªãŒãã£ã³ã°ãšãã§ãªãŒæ¬åèª
ðŸ Q32: Rubyã®yieldã«ã€ããŠèª¬æããŠãã ãã
yirld
ã¯ãã¡ãœããå®çŸ©ã®å€åŽã«ãããããã¯ãæé»çã«å©çšãã
def puts_stuff
puts 'first line'
yield if block_given?
puts 'third line'
yield if block_given?
end
puts_stuff { puts 'its me' }
# => first line
# => its me
# => third line
# => its me
issue09
æœè±¡æŠå¿µãšããŠã¯ç解ãããå
±åãç解ãããã ããŸã ãã©ãã¯ããã¯ã¹ã®ãŸãŸã解決ãã
content_for
ã®äœ¿ãã¿ã¡ã説æããŠãã ãã
Q33:Â content_for
ã¯ããã¥ãŒå
ã§ã³ã³ãã³ããå®çŸ©ãããã¬ã³ããªã³ã°ãããã§ãã
ããã¯ã³ã³ãã³ãã1ç®æã§å®çŸ©ããŠããŸããŸãªå Žæã§ã¬ã³ããªã³ã°ããã®ã«äŸ¿å©
ããtitleã¿ã°ãšãã§äœ¿çšããã
ðŸ Q34: HashãšJSONã®éãã説æããŠãã ãã
Hash
ã¯Ruby
ã®ã¯ã©ã¹ã§ãããããŒãæå®ããŠå€ã«ã¢ã¯ã»ã¹ã§ããããŒããªã¥ãŒãã¢ã®ã³ã¬ã¯ã·ã§ã³ïŒäžèŠ§ã¿ãããªæå³åãïŒ
JSON
ã¯ãããŒã¿éä¿¡ã«çšããç¹å®ã®ãã©ãŒããããæã€æåå
ðŸ Q35: Active Jobã«ã€ããŠèª¬æããŠãã ãã
Active Job
ãšã¯ãéåæåŠçæ©èœãæäŸããã©ã€ãã©ãª
Active Jobã®å
žåçãªå©çšæ³ã¯ãã¡ã€ã³ã®Webã¹ã¬ããã§å®è¡ããå¿
èŠã®ãªãã³ãŒãã®å®è¡ ã§ã
ãŠãŒã¶ãŒã«ã¡ãŒã«éç¥ãéä¿¡ãããšãã«ãã䜿ããã
2022/4/5 ä»ã®ç解 : Active Jobã¯ããæ¹ïŒæœè±¡ïŒã§sidekiqïŒå ·äœïŒã§ãã
issue10
ãã³ãºãªã³ïŒãRails
ïŒ
ðŸ Q36: Railsã®ã©ããããšããã奜ãã§ããïŒ
- ã©ã€ãã©ãªãè±å¯ïŒGemã®çš®é¡ãå€ãïŒ
-
Active Record
ãåªç§ã§ããïŒæ¯èŒã¯ããŠããªãïŒ
railsãçæéã§éçºãã§ããçç±ã¯ïŒ
- ã¡ãœãããè±å¯ã§æèŠçã«äœ¿ããïŒTime.nowãªã©ïŒ
- Rubyã»Railsã®ã³ãã¥ããã£ãå€ã
ðŸ Q37: Railsã®ã©ããããšããããã©ã€ã§ããïŒ
ã ãã調ã¹ããšå€§èŠæš¡éçºã§ã¯ä»¥äžã®ãããªåé¡ããã£ã
- èšå®ããèŠçŽãšããããšããããæè»æ§ã¯ããŸããªã
- ããã©ãŒãã³ã¹ãé ãïŒã¹ã±ãŒãªã³ã°ãå°é£ïŒå®è¡é床ãé ãããïŒïŒ
ðŸ Q38: ãèŽå±ïŒãã²ããïŒã®Ruby gemãæããŠäžãã
èŠèª¿æ»
issue11
調æ»
ðŸ Q39: springã«ã€ããŠèª¬æããŠãã ãã
spring ã¯ãã¢ããªã±ãŒã·ã§ã³ãããã¯ã°ã©ãŠã³ãã§å®è¡ãç¶ããããšã§éçºãã¹ããŒãã¢ãããã Railsã¢ããªã±ãŒã·ã§ã³ããªããŒããŒ
ðŸ Q40: ã¢ã»ãããã€ãã©ã€ã³ã«ã€ããŠèª¬æããŠãã ãã
JS
ãCSS
ãªã©ãå¹ççã«æ±ãããã®ä»çµã¿
éçºè ãæžããJSãCSSãããã©ãŠã¶ã§èªã¿åãã圢åŒã§ãå®è¡é床ãæ©ãããã©ãŠã¶ãã£ãã·ã¥ã«å¯ŸããŠæé©åããããã®ãã€ãã©ã€ã³åŠçãè¡ã
ããã£ãã·ã¥ããšã¯ããã©ãŠã¶ãªã©ãã衚瀺ãããŠã§ãããŒãžã®ããŒã¿ãäžæçã«ã³ã³ãã¥ãŒã¿ãŒã«ä¿åããŠããæ©èœã§ãã 次ã«åãããŒãžã«ã¢ã¯ã»ã¹ãããšãã«ãã€ã³ã¿ãŒãããäžã®ããŒã¿ã§ã¯ãªããä¿åãããããŒã¿ãåç §ããããããã°ãã衚瀺ã§ãã
ðŸ Q41: Railsã§èªèšŒã管çãããšãã«ã¯äœã䜿ã£ãŠããŸããã
sorcery
ðŸ Q42: splatæŒç®åã«ã€ããŠèª¬æããŠãã ãã
é åèŠçŽ ãä»ã®å€æ°ããã¡ãœããã®åŒæ°ã®äžéšãšããŠæ±ãããšãã§ããããã«ãªããã®
issue12
ã³ãŒããªãŒãã£ã³ã°
ðŸ Q43: includeãšextendã®éãã説æããŠãã ãã
include
ãšextend
ã¯ã©ã¡ããããã¯ã¹ã€ã³
ðŸ Q44: loadãšrequireã®éãã説æããŠãã ãã
load
: å¥ã®ãã¡ã€ã«ãèªã¿èŸŒãïŒæ¢ã«ã¡ã¢ãªäžã«èªã¿èŸŒãŸããŠããå Žåã§ãå®è¡ããïŒ
require
: å¥ã®ãã¡ã€ã«ã1床ã ãå®è¡ããïŒäœåºŠrequireããŠãåãïŒ
ðŸ Q45: ã¯ã©ã¹ãšã¢ãžã¥ãŒã«ã®éãã説æããŠãã ãã
- ã¯ã©ã¹ã«ã¯å±æ§ãšã¡ãœããã1ã€ä»¥äžãããã¯ã©ã¹ã¯ã€ã³ã¹ã¿ã³ã¹ãäœæã§ãã
- ã¢ãžã¥ãŒã«ã¯åãªãã¡ãœãããšå®æ°ã®ã³ã¬ã¯ã·ã§ã³ã§ãããä»ã®ã¢ãžã¥ãŒã«ãã¯ã©ã¹ã«ããã¯ã¹ã€ã³ã§ãã
issue12
åèª
ðŸ Q46: Active Recordã®ã¹ã³ãŒãã«ã€ããŠèª¬æããŠãã ãã
Active Recordã®ã¹ã³ãŒãïŒscope
ïŒã¯ãActive Recordã¢ãã«å
ã§å®çŸ©ãããä»ã®ã©ãããã§ãåŒã³åºããã¯ãšãªããžãã¯
# å
·äœäŸ
class Post
scope :active_posts, -> { where(active:true) }
end
issue13
äžèšãšã¯çŽæ¥çã«é¢ä¿ã¯ãªããäžèªãã
ðŸ Q47: ã¯ã©ã¹å€æ°ãšã€ã³ã¹ã¿ã³ã¹å€æ°ã®éãã説æããŠãã ãã
@
ããŒã¯1ã€
ã€ã³ã¹ã¿ã³ã¹å€æ° :
@@
ããŒã¯2ã€
ã¯ã©ã¹å€æ° : ðŸ Q48: Active Recordã®findãfind_byãwhereã®éãã説æããŠãã ãã
find
: åã¢ãã«ã®id
ãæ€çŽ¢ããŒãšããŠããŒã¿ãååŸããã¡ãœãã
find_by
: åã¢ãã«ãid以å€ã®æ¡ä»¶ã§
æ€çŽ¢ããã¡ãœãã(idã§ãæ€çŽ¢å¯èœ)
where
: åã¢ãã«ãid以å€ã®æ¡ä»¶ã§æ€çŽ¢ããå Žå該åœããããŒã¿å
šãŠãè¿ã
ðŸ Q49: selectãšmapãšcollectã®éãã説æããŠãã ãã
select
: ã³ã¬ã¯ã·ã§ã³ã®ãµãã»ãããååŸããã®ã«äœ¿ãã!
ä»ãã®select!
ãåŒã¶ãšãå
ã®ã³ã¬ã¯ã·ã§ã³ãæ¹å€ããã
i = [1,2,3,4,5]
i.select {|x| x % 2 == 0}
# => [2, 4]
map
: ã³ã¬ã¯ã·ã§ã³ã®åèŠçŽ ã«å¯ŸããŠæäœãå®è¡ããæŽæ°ãããã³ã¬ã¯ã·ã§ã³ãåºåããŸãã!
ä»ãã®map!
ãåŒã¶ãšãæ¬ã®ã³ã¬ã¯ã·ã§ã³ãæ¹å€ããã
i = [1,2,3,4,5]
i.map {|x| x+1}
# => [2,3,4,5,6]
issue14
æ¥æ¬èªãããã«ããã®ã§ä¿®æ£
ðŸ Q50: Railsã®CRUD verbãšãããã«å¯Ÿå¿ããã¢ã¯ã·ã§ã³ãè¿°ã¹ãŠãã ãã
HTTP | verb | ãã¹ | ã³ã³ãããŒã©#ã¢ã¯ã·ã§ã³ |
---|---|---|---|
GET | /users | users#index | ãã¹ãŠã®ãŠãŒã¶ãŒäžèŠ§ã衚瀺 |
GET | /users/new | users#new | ãŠãŒã¶ãŒã1人äœæããããã®HTMLãã©ãŒã ãè¿ã |
POST | /users | users#create | ãŠãŒã¶ãŒã1人äœæãã |
GET | /users/:id | users#show | ç¹å®ã®ãŠãŒã¶ãŒã衚瀺ãã |
GET | /users/:id/edit | users#edit | ãŠãŒã¶ãŒç·šéçšã®HTMLãã©ãŒã ã1ã€è¿ã |
PATCH/PUT | /users/:id | users#update | ç¹å®ã®ãŠãŒã¶ãŒãæŽæ°ãã |
DELETE | /users/:id | users#destroy | ç¹å®ã®ãŠãŒã¶ãŒãåé€ãã |
ðŸ Q51: createã¢ã¯ã·ã§ã³ãžã®ã«ãŒãã£ã³ã°ããresourcesã䜿ããªãã§å®çŸ©ããŠãã ãã
resources
ããã®å Žå : resources :photos
resources
ãªãã®å Žå : post â/photosâ, to: âphotos#createâ, as: :create_photo
resources :magazines do
resources :ads, as: 'periodical_ads'
end
# magazine_periodical_ads_urlã¿ãããªã«ãŒãã£ã³ã°ãã«ããŒã«ãªã
ðŸ Q52: Rubyã®3段éã®ã¢ã¯ã»ã¹å¶åŸ¡ãè¿°ã¹ãŠãã ãã
public
: ãã®ã¡ãœããã¯ä»»æã®ãªããžã§ã¯ãããåŒã³åºãã
protected
: ãã®ã¡ãœããã¯ãã®ã¡ãœãããå®çŸ©ãããŠããã¯ã©ã¹ãšãã®ã¯ã©ã¹ã®ãµãã¯ã©ã¹ããããåŒã³åºããªã
private
: ãã®ã¡ãœããã¯ãã®ãªããžã§ã¯ãèªèº«ããåŒã³åºããªã
ðŸ Q53: Rubyã®ã·ã³ã°ã«ãã³ã®äœ¿ãã¿ã¡ã説æããŠãã ãã
ã·ã³ã°ã«ãã³ã¯ãã¯ã©ã¹ã«ã€ã³ã¹ã¿ã³ã¹ã1ã€ããæãããªããã¶ã€ã³ãã¿ãŒã³
- ãã°ã®æžèŸŒåŠçãè¡ãã¡ãœããã§ã®ãã¡ã€ã«ãžã®ã¢ã¯ã»ã¹
- ã·ã¹ãã å ã§å ±éã®ãã£ãã·ã¥ããŒãã«ãåç §ããå Žå