How to use belongs_to in Ruby on Rails

Rails 4:
Let’s assume we have two models Movie and Actor. Actor has many movies and Movie belongs to Actor. In most of the cases, the developers used to validate the foreign key actor_id is present or not. In this case, it will not validate actor model as to whether the entered actor id exists or not. It is just like attribute validation which always validates that the fields should be non-empty when submitting the form.

Validation with foreign key:

class Movie < ApplicationRecord
belongs_to :actor
validates :actor_id, presence: true
end
class Actor < ApplicationRecord
has_many :movies, dependent: :destroy
end

The scenario above, validates actor id presence or not like normal attribute presence validator.

Ex: Actor.create(title: “abc”).
=> {id: 1, title: 'abc'}
m = Movie.new(title: “ok ok”, actor_id: 111)
=> m.valid? => true
=> Actor.find(111)
ActiveRecord::RecordNotFound: Couldn't find Actor with 'id'=111

We can still save the movie record without valid actor.

With associations

class Movie < ApplicationRecord
belongs_to :actor
validates :actor, presence: true
end

(or)

class Movie < ApplicationRecord
belongs_to :actor, required: true
end
class Actor < ApplicationRecord
has_many :movies, dependent: :destroy
end
Ex: Actor.create(title: “abc”).
==> {id: 1, title: 'abc'}
m = Movie.new(title: “ok ok”, actor_id: 111)
==> m.valid? => false
==> m.errors.full_messages, ['Actor can't be blank']

In this case, it will always validate whether the entered actor exists or not. In case of an invalid actor it throws error to you. This is the best practise to make your associations. It always checks for the associated object exists or not.

Schienen5

From rails5 these validations were added by default. It validates association object should be present when you define belongs_to associations.

Release notes

http://guides.rubyonrails.org/5_0_release_notes.html(belongs_to will now trigger a validation error by default if the association is not present.)

We can opt out of this feature completely from the application by setting config opton in initializer file.

config/initializers/new_framework_defaults.rb
Rails.application.config.active_record.belongs_to_required_by_default = false

This initializer file is present in rails5 application only. Need to add this initializer file manually when you migrate from older version of your rails application and make necessary changes.

class Post < ApplicationRecord
has_many :comments, dependent: :destroy	
end
class Comment < ApplicationRecord
belongs_to :post
end
c = Comment.create(title: “awesome post”)
c.errors.full_messages.to_sentence
=> “Post must exist”

We can not create any comment record without an associated record.

We can opt out this default behaviour by setting

belongs_to :post, optional: true
c = Comment.create(title: “awesome post”)
=> 

RailsCarma hat angeboten Ruby on Rails-Entwicklungsdienste seit über 8 Jahren. Unsere Entwickler sind mit der Ausführung aller Arten von Aufgaben bestens vertraut Ruby on Rails-Webentwicklung Projekte und Erweiterung der Funktionen Ihrer vorhandenen Rails-Anwendungen. Kontaktieren Sie uns, um mehr über unsere Entwicklungskompetenzen und die Projekte zu erfahren, an denen wir gearbeitet haben.

zusammenhängende Posts

Hinterlasse einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

de_DEGerman