Recently I switched to Rails 5.0.0.1 and I don’t quite understand some of the things associated with Active Record associations. Or I’m missing something (I haven’t taken the rails for a long time).
In the 4th version of the rail, I could safely do the following steps without receiving a single error.
rails g model Parent name:string rails g model Child name:string parent:references rake db:migrate rails c
Parent.create name: "Papa" p = Parent.last Child.create name: "Son" c = Child.last After these simple actions, you could safely perform the following actions:
p.children << c # Добавить "Son" к "Papa" p.children # Вывести список всех дочерних записей c.parent # Посмотреть родителя c c.parent = p # Напрямую установить родителя для c It was all pretty obvious and convenient.
But now I have updated to the 5th version of the rail and do not quite understand what is happening.
First, I performed the standard operations:
rails g model Parent name:string rails g model Child name:string parent:references rails db:migrate Nothing new.
rails c
Parent.create name: "Papa" p = Parent.last Child.create name: "Son" # * And in line (*) I stumble upon something . What exactly - I can not say for sure. Specifically, the following happens:
(0.1ms) begin transaction
(0.1ms) rollback transaction
Apparently, the fact is that I did not indicate to which parent the offspring belong, because if you do something like
p.children.create name: "Son" Then I don’t receive any rollbacks and the transaction is executed.
Actually, the question is: am I missing / doing something wrong, or is it really no longer possible to create a model that is the heir to another model, without indicating the parent? And if so, is there any way to restore the previous behavior? After all, it may be that there are many child models and many parents, but not all child models are used. How then to create them?
Child.create(name: "Son").errors.messages=>{:parent=>["must exist"]}- D-side