๐Ÿชด HA's Garden

Search

SearchSearch
  • ๐ŸŒฟ What?
  • ๐ŸŒฟ Refer

๐ŸŒฑ Validate number of association

Sep 29, 2022, 1 min read

  • #til
  • #rails

๐ŸŒฟ What? ยง

  • ๐ŸŒฑ If you have a People model and a Vehicle model, every people has_many vehicles but you want to set maximum for number of vehicles which a people can has. You can use following command below:
# vehicle.rb
class Vehicle
  belongs_to :people
end
 
# people.rb
class People
  has_many :vehicles
 
  validates :vehicles, length: { maximum: 2 }
end

๐ŸŒฟ Refer ยง

https://til.hashrocket.com/posts/egegrgsdnj-limiting-object-counts-in-rails-associations-

Graph View

Backlinks

  • No backlinks found

Created with Quartz v4.0.10, ยฉ 2025

  • GitHub
  • Discord Community