Flowdock
method

check_box

Importance_1
v4.0.2 - Show latest stable - 2 notes - Class: CheckBoxBuilder
check_box(extra_html_options={}) public

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Show source
Register or log in to add new notes.
December 30, 2016
0 thanks

Used within collection_check_boxes

Note the checked: option.

%table
  = collection_check_boxes(:ahj_types, :ids, AhjType.order(:TypeName), :AHJTypeID, :TypeName) do |b|
    %tr
      %td{style: 'padding: 0 1em;'}
        = b.label(class: "check_box")
      %td{style: 'padding: 0 1em;'}
        = b.check_box(class: "check_box", checked: (params[:ahj_types][:ids].member?(b.value.to_s)))
January 24, 2017 - (v4.0.2 - v4.2.7)
0 thanks

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 Movie < ApplicationRecord
belongs_to :actor
validates :actor_id, presence: true
end

class Actor < ApplicationRecord
has_many :movies, dependent: :destroy
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

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

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

(or)

class Movie < ApplicationRecord
belongs_to :actor, required: true
end

class Movie < ApplicationRecord
belongs_to :actor, required: true
end

class Actor < ApplicationRecord
has_many :movies, dependent: :destroy
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']

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.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

config/initializers/new_framework_defaults.rb

Rails.application.config.active_record.belongs_to_required_by_default = false 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 Post < ApplicationRecord
has_many :comments, dependent: :destroy	
end

class Comment < ApplicationRecord
belongs_to :post
end

class Comment < ApplicationRecord
belongs_to :post
end

c = Comment.create(title:awesome post”)
c.errors.full_messages.to_sentence
=>Post must exist”

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
belongs_to :post, optional: true
c = Comment.create(title:awesome post”)
c = Comment.create(title:awesome post”)
=> <Comment id: 1, title:awesome post”, post_id: nil>

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