Jump to content

The ultimate community for Ruby on Rails developers.


Photo

How to use belongs_to in Ruby on Rails

Ruby on Rails belongs_to

  • Please log in to reply
No replies to this topic

#1 rubyonrailsdevelopment

rubyonrailsdevelopment

    Signalman

  • Members
  • 23 posts
  • LocationUS California

Posted 07 December 2016 - 10:23 AM

How-to-use-belons_to-in-ruby-on-rails.pn

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.

Rails5
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.rubyon...ease_notes.html

(belongs_to)  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”)
  => <Comment id: 1, title: “awesome post”, post_id: nil>

 

Read More >>> http://www.railscarma.com/blog/technical-articles/use-belongs_to-ruby-rails/







Also tagged with one or more of these keywords: Ruby on Rails, belongs_to

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users