Hi,

Suppose I have a model class which has a time field:

class CreateAppointments < ActiveRecord::Migration
def change
create_table :appointments do |t|
t.string :name
t.datetime :startTime
t.datetime :endTime
t.string :description

t.timestamps
end
end
end

When I test drive it in rails console, I can input any value int he
startTime and endTime such as:

a = Appointment.new()
a.startTime = 1234
a.endTime = 5678
a.save()

My question is does Rails support some basic data type validation for us
so that we can always input a correct format?

Thanks,
Kahou

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Colin Law at Dec 10, 2011 at 5:43 pm

    On 10 December 2011 17:34, kahou l. wrote:
    Hi,

    Suppose I have a model class which has a time field:

    class CreateAppointments < ActiveRecord::Migration
    def change
    create_table :appointments do |t|
    t.string :name
    t.datetime :startTime
    t.datetime :endTime
    t.string :description

    t.timestamps
    end
    end
    end

    When I test drive it in rails console, I can input any value int he
    startTime and endTime such as:

    a = Appointment.new()
    a.startTime = 1234
    a.endTime = 5678
    a.save()

    My question is does Rails support some basic data type validation for us
    so that we can always input a correct format?
    The way to do it is to always go via a Time object. If the user is
    entering data then parse that into a Time object then save that. If
    you need to check that the time is in a particular range for example
    then also include a validation in the model.

    Colin

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedDec 10, '11 at 5:35p
activeDec 10, '11 at 5:43p
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Kahou l.: 1 post Colin Law: 1 post

People

Translate

site design / logo © 2021 Grokbase