yutokyokutyo / rebuild_sample_app

https://railstutorial.jp/chapters/static_pages?version=5.0#cha-static_pages
https://rebuildsampleapp.herokuapp.com/
5 stars 0 forks source link

valid? って何者なの? #10

Closed yutokyokutyo closed 7 years ago

yutokyokutyo commented 7 years ago

問題解決方法を学ぶ目的で調査した。

yutokyokutyo commented 7 years ago

問題の認識 → 問題解決のための計画を立てる → 実行する → 結果を確認する・ふりかえる

yutokyokutyo commented 7 years ago

img_6442 img_4585

yutokyokutyo commented 7 years ago

valid? をすると呼ばれる app/models/user.rb を見ると ApplicationRecord を継承していた。

class User < ApplicationRecord

app/models/application_record.rb を見ると特にvaid?による記述はなくActiveRecord::Baseを継承していた。

class ApplicationRecord < ActiveRecord::Base

どうやらRailsのActiveRecordでもともと定義されているっぽい。

Rails のコードを手元に落としてgrep この中のどれかの valid? が呼ばれている。

% pt 'def valid?'                                                                                                                        (git)-[master]
actioncable/lib/action_cable/connection/message_buffer.rb
38:        def valid?(message)

activerecord/test/cases/validations/association_validation_test.rb
50:      def valid?

guides/source/api_documentation_guidelines.md
261:def valid?(context = nil)

actionpack/lib/action_controller/metal.rb
22:      def valid?(action)

activerecord/lib/active_record/validations.rb
63:    def valid?(context = nil)

activemodel/lib/active_model/validations.rb
332:    def valid?(context = nil)

activerecord/lib/active_record/dynamic_matchers.rb
59:        def valid?

rbenv version してみると system の ruby を見ていた。

project 内で install した gem である activerecord で呼ばれているはず。

% gem which rails                                                                                                               (git)-[Exercises-7.1.2]
/usr/local/lib/ruby/gems/2.3.0/gems/railties-5.0.2/lib/rails.rb
% gem which activerecord                                                                                                        (git)-[Exercises-7.1.2]
ERROR:  Can't find ruby library file or shared library activerecord

該当箇所(valid? の定義元)に p '文字列' をしてみたが応答はなかった。

% rails c                                                                                                                       (git)-[Exercises-7.1.2]
Running via Spring preloader in process 95513
Loading development environment (Rails 5.0.2)
>> user = User.new(name: 'yuto', email: 'email@gmail.com')
=> #<User id: nil, name: "yuto", email: "email@gmail.com", created_at: nil, updated_at: nil, password_digest: nil>
>> user.valid?
  User Exists (0.2ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(?) LIMIT ?  [["email", "email@gmail.com"], ["LIMIT", 1]]
=> false
kurotaky commented 7 years ago

あともう一歩で問題解決できそうだから、明日も引続きやっていこう 💪

yutokyokutyo commented 7 years ago

はい!!うおおお!!

yutokyokutyo commented 7 years ago

該当箇所

yutokyokutyo commented 7 years ago

でない。。。

% rails c                                                                                                                                         (git)-[Exercises-7.1.2]
Running via Spring preloader in process 42823
Loading development environment (Rails 5.0.2)
>> user = User.new(name: "yuto", email: "k.yuto.cre@gmail.com")
=> #<User id: nil, name: "yuto", email: "k.yuto.cre@gmail.com", created_at: nil, updated_at: nil, password_digest: nil>
>> user.valid?
  User Exists (0.2ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(?) LIMIT ?  [["email", "k.yuto.cre@gmail.com"], ["LIMIT", 1]]
=> false

spring を stop すると...

% spring stop                                                                                                                                     (git)-[Exercises-7.1.2]
Spring stopped.

出た!!

% rails c                                                                                                                                         (git)-[Exercises-7.1.2]
Running via Spring preloader in process 42942
Loading development environment (Rails 5.0.2)
>> user = User.new(name: "yuto", email: "k.yuto.cre@gmail.com")
=> #<User id: nil, name: "yuto", email: "k.yuto.cre@gmail.com", created_at: nil, updated_at: nil, password_digest: nil>
>> user.valid?
"真実はいつもひとつ!"
"Activemodelだよーん"
  User Exists (0.2ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(?) LIMIT ?  [["email", "k.yuto.cre@gmail.com"], ["LIMIT", 1]]
=> false

spring とは一体...?

yutokyokutyo commented 7 years ago

http://ruby-rails.hatenadiary.com/entry/20141026/1414289421

なるほど spring!!

メソッドのおいかけ方はこんなものもある!

メソッドの定義元クラス
helper.method(:link_to)
#=> <Method: ActionView::Base(ActionView::Helpers::UrlHelper)#link_to>
どのファイル?
helper.method(:link_to).source_location
#=> ["/Users/hoge/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/actionpack-4.0.2/lib/action_view/helpers/url_helper.rb", 174]
yutokyokutyo commented 7 years ago

人に魚を与えればそれは一日の糧となる。しかし魚の釣り方を教えればそれは一生の糧となる。

yutokyokutyo commented 7 years ago

こうしてライブラリのコードを追いかける方法と問題解決方法を学んだのであった。 issue には雑にしかメモしてないけど。完!