'dependent' is an option used with association in rails.

An association is the relationship between models. There are associations like one-one, one-many, many-many. To represent association in rails, it uses has_many, belongs_to, has_one and has_and_belongs_to_many.

For Eg: Account model contains many Members. It is represented as:

In Account Model:
has_many :members

In Member Model:
belongs_to :account

In the above example if we delete one record from the accounts table it will affect the members table. The members table will contain the invalid account_id. In these situations we can use 'dependent'.

In Account Model:
has_many :members, :dependent => :destroy

It will automatically deletes all the dependent data from the models.

blog comments powered by Disqus
Delayed Job →