Created
May 17, 2017 08:24
-
-
Save willnet/c6bfb94d79763b231eac1ccc4a2108ee to your computer and use it in GitHub Desktop.
inverse_of が必須なケース
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class User < ApplicationRecord | |
| has_one :post | |
| end | |
| class Post < ApplicationRecord | |
| belongs_to :fuga, class_name: 'User' | |
| end | |
| u = User.new | |
| post = u.build_post | |
| post.valid? #=> false | |
| post.errors #=> #<ActiveModel::Errors:0x007f9b5caf2400 @base=#<Post id: nil, user_id: nil, title: nil, body: nil, created_at: nil, updated_at: nil>, @messages={:fuga=>["must exist"]}, @details={:fuga=>[{:error=>:blank}]}> | |
| post.fuga #=> nil | |
| # 次のようにすると valid? が true になる | |
| class User < ApplicationRecord | |
| has_one :post, inverse_of: :fuga | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment