How it works - `belongs_to: user dependent :: destroy`

I know how to work has_many :posts, dependent: :destroy. If Useror something that the has_manymessage is destroyed, all owned messages are also destroyed.

But what happens when Postmodel belongs_to :user, dependent: :destroy? I found an option in the Rails Guide, but I could not find out how it works.

http://guides.rubyonrails.org/association_basics.html

+4
source share
2 answers
"has_many" 

"has_many" . , . , teacher_id, , .

"belongs_to" 

"" . , . , , , .

, /.

class Teacher < ActiveRecord::Base
  has_many :students, dependent: :destroy
end

class Student < ActiveRecord::Base
    belongs_to :teacher 
end 

,

Teacher.destroy 

, .

Teacher.find(345).destroy 

345 .

, , , ?

class Teacher < ActiveRecord::Base
  has_many :students, dependent: :destroy
end

class Student < ActiveRecord::Base
    belongs_to :teacher, dependent: :destroy
end 

Student.destroy

, . , (, ), , , "".

Ruby 1

: destroy, , . , property_to has_many - .

+10

, . , .

+1

Source: https://habr.com/ru/post/1623014/


All Articles