what is the best way to not create/save if exist? i have the following in my models
class Censu < ApplicationRecord
before_create :check_if_exist
def check_if_exist
break if ItemsCensu.exists?(year: @year)
puts "Εχεις κανει απογραφη για την χρονια #{@year}"
end
end
i try render:index but still not work, also break
if i put break i have the following error
Invalid break so how can i stop the create if the year exist? and put the puts message as an alert or error?
just change break with return
class Censu < ApplicationRecord
before_create :check_if_exist
def check_if_exist
return if ItemsCensu.exists?(year: @year)
// your other code here
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.