Validate graduation date in Rails 5 model - ruby-on-rails

I am trying to make sure that users have a valid graduation date within my application. Not sure if my variable for the current_year will be updated every time I want to validate a user's graduation date. Here is what I have (I am making this field optional for users):
current_year = Time.now.year
if !(:grad_year.nil?)
validates :grad_year, numericality: { greater_than_or_equal_to: current_year, less_than_or_equal_to: current_year + 5 }
end

validates :grad_year, numericality: { greater_than_or_equal_to: Date.today.year, less_than_or_equal_to: Date.today.year + 5 }, allow_nil: true

Actually, it is not correct to use Date.today.year for validation because the current year will be hardcoded on Rails start and won't update next year (until you restart Rails).
Use Lambda instead:
validates :grad_year, numericality: { greater_than_or_equal_to: -> { Date.today.year }, less_than_or_equal_to: -> { Date.today.year + 5 } }, allow_nil: true

Related

Rails validation integer value grater then if other field value

I want to check an integer value in validation. But I want to do it depending on the value of another field. For example, if the width value exists, let it be at least 300 if the width_type is "px", but 30 if the width_type is "rate". How can I do it thanks.
If I understand correctly you are looking for conditional validation
In that case the following should work:
validates :width, numericality: { only_integer: true}
validates :width, numericality: { greater_than_or_equal_to: 300}, if: -> {width_type.to_s == 'px'}
validates :width, numericality: { greater_than_or_equal_to: 30}, if: -> {width_type.to_s == 'rate'}
If you need to handle too many other scenarios I would go with a custom validation like
MIN_WIDTH_PER_TYPE = {px: 300, rate: 30}
validates :width, numericality: { only_integer: true}
validate :_width_with_type
private
def _width_with_type
min_width = MIN_WIDTH_PER_TYPE[width_type.to_sym]
if width < min_width
errors.add(:width, "must be at least #{min_width} when using width type '#{width_type}'")
end
end
You could clean this up further as well but this should point you in the right direction.

Validate only numbers

In one of my rails models I have this :only_integer validation:
validates :number, presence: true, numericality: { only_integer: true }
This validation also allows inputs like +82938434 with +-signs.
Which validation should I use to only allow inputs without + - only numbers?
The documentation for only_integer mentions this regex :
/\A[+-]?\d+\z/
It means you could just use:
validates :number, format: { with: /\A\d+\z/, message: "Integer only. No sign allowed." }
Rails 7 added :only_numeric option to numericality validator
validates :age, numericality: { only_numeric: true }
User.create(age: "30") # failure
User.create(age: 30) # success

When validating parameters for a Rails model, can you check one param's value in relation to another?

In my current Rails project, I have a model called "Child," with two parameters, "choc_cupcake" and "cherry_cupcake."
Each child should be allowed to have up to 5 cupcakes total, in any combination of chocolate and/or cherry cupcakes. So far, my model validation code just allows each child to have up to 5 chocolate cupcakes AND up to 5 cherry cupcakes.
class Child < ActiveRecord::Base
validates :choc_cupcake, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 5 }
validates :cherry_cupcake, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 5 }
end
Is there a way to specify that if a child chooses X chocolate cupcakes, then he/she can have no more than (5-X) cherry cupcakes? Something like this, maybe?
validates :cherry_cupcake, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: (5 - :choc_cake) }
class Child < ActiveRecord::Base
validates :choc_cupcake, numericality: { only_integer: true, greater_than_or_equal_to: 0}
validates :cherry_cupcake, numericality: { only_integer: true, greater_than_or_equal_to: 0}
validate :no_more_than_5_total_cupcakes
private
def no_more_than_5_total_cupcakes
if (choc_cupcake + cherry_cupcake) > 5
message = "No more than 5 total cupcakes!"
errors.add(choc_cupcake: message)
errors.add(cherry_cupcake: message)
end
end
end

Rails Model Validations

I have a model and adding some validations
This is what I originally had:
validates :speed,
allow_blank: true,
numericality: { only_integer: true, greater_than: 0 }
But I keep getting errors when importing items from my CSV file stating that
Speed must be an integer
I then changed it to:
validates :speed,
numericality: { only_integer: true, greater_than: 0 }, unless: "speed.nil?"
But I get the same errors here too.
Basically I want it to validate that speed is numeric and greater than 1 unless no speed is passed in and to allow that blank value.
Any ideas?
CSV Importer:
def self.import_from_csv(file)
Coaster.destroy_all
csv_file = CSV.parse(
File.read(
file.tempfile,
{encoding: 'UTF-8'}
),
headers: true,
header_converters: :symbol
)
csv_file.each do |row|
coaster_name = row[:name]
# No need to keep track of coasters already in the database as the CSV only lists each coaster once unlike parks
# create the new coaster
park = Park.find_by_name_and_location_1(row[:park], row[:location_1])
manufacturer = Manufacturer.find_by_name(row[:manufacturer])
coaster = Coaster.create!({
name: row[:name],
height: row[:height],
speed: row[:speed],
length: row[:length],
inversions: row[:inversions] == nil ? 0 : row[:inversions],
material: (row[:material].downcase if row[:material]),
lat: row[:coaster_lat],
lng: row[:coaster_lng],
park_id: park.id,
notes: row[:notes],
powered: row[:powered],
manufacturer_id: (manufacturer.id if manufacturer),
covering: row[:covering],
ride_style: row[:ride_style],
model: row[:model],
layout: row[:layout],
dates_ridden: row[:dates_ridden],
times_ridden: row[:times_ridden],
order: row[:order],
on_ride_photo: row[:on_ride_photo] == 1 ? true : false,
powered: row[:powered] == 1 ? true : false
})
ap "Created #{row[:name]} at #{row[:park]}"
end
end
I think the value for speed from csv is interpreted as string. You may use .to_i with that specific value that you are using for speed. Change your code like this:
park = Park.find_by_name_and_location_1(row[:park], row[:location_1])
manufacturer = Manufacturer.find_by_name(row[:manufacturer])
row_speed = row[:speed].blank? ? nil : row[:speed].to_i
coaster = Coaster.create!({
.....
speed: row_speed,
.....
})
And then in validation:
validates :speed, numericality: { only_integer: true, greater_than: 0 }, allow_nil: true
Validations accept an :allow_nil argument, as noted here in the Rails guides: http://edgeguides.rubyonrails.org/active_record_validations.html#allow-nil
If the attribute is nil when :allow_nil is true, that particular validation will only run if the attribute in question is present.
I think, validates numericality accepts allow_nil attribute. Try this:
validates :speed, numericality: { only_integer: true, greater_than: 0 }, allow_nil: true

Best way to validate year via Ruby on Rails validates method?

Currently I have a function to check if the birthyear is correct:
validates :birth_year, presence: true,
format: {with: /(19|20)\d{2}/i }
I also have a function that checks if the date is correct:
validate :birth_year_format
private
def birth_year_format
errors.add(:birth_year, "should be a four-digit year") unless (1900..Date.today.year).include?(birth_year.to_i)
end
Is it possible to combine the bottom method into the validates at the top instead of the two validates I have now?
You should be able to do something like this:
validates :birth_year,
presence: true,
inclusion: { in: 1900..Date.today.year },
format: {
with: /(19|20)\d{2}/i,
message: "should be a four-digit year"
}
Take a look at: http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates
:birth_year, presence: true,
format: {
with: /(19|20)\d{2}/i
}
numericality: {
only_integer: true,
greater_than_or_equal_to: 1900,
less_than_or_equal_to: Date.today.year
}
regex
/\A(19|20)\d{2}\z/
will only only allow numbers between 1900 e 2099
\A - Start of string
\z - End of string

Resources