私は2つのモンゴイドクラスを持っています:
class Reservation
include Mongoid::Document
belongs_to :listing, class_name: 'Listing', inverse_of: 'Reservation'
end
class Listing
include Mongoid::Document
has_many :reservations, class_name: 'Reservation', foreign_key: :listing_id
end
リストが見つかった場合_id
、#save!
エラーは発生しません
listing = Listing.find(...)
listing.save! #=> true
次に、新しい予約オブジェクトを初期化します。
reservation = Reservation.find_or_initialize_by(params)
reservation.save! #=> error, exptected though, because I didn't set the listing_id yet
Mongoid::Errors::Validations:
message:
Validation of Reservation failed.
summary:
The following errors were found: Listing can't be blank
resolution:
Try persisting the document with valid data or remove the validations.
from /home/ec2-user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/mongoid-71c29a805990/lib/mongoid/persistable.rb:78:in `fail_due_to_validation!'
そこで、以前のリスティングの ID を予約に割り当てます。
reservation.listing_id = listing._id
reservation.listing_id #=> nil
listing_id フィールドを割り当てることさえできませんか?!
reservation.listing #=> returns the associated document no problem though..
reservation.listing.save! #=> error
Mongoid::Errors::Validations:
message:
Validation of Listing failed.
summary:
The following errors were found: Reservations is invalid
resolution:
Try persisting the document with valid data or remove the validations.
from /home/ec2-user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/mongoid-71c29a805990/lib/mongoid/persistable.rb:78:in `fail_due_to_validation!'
有効なリスティングがないと予約を保存できません, 有効な予約がないとリストを保存できません
これは何ですか?!?!
私の一日を救ってください...