Hello everyone! I want to add a column to a table. So here is my migration. My question is : Is it possible to say that this value must be between 1 and 7 and must no be a float (1.4 shouldn''t be possible) ? class AddValueToTable < ActiveRecord::Migration def self.up add_column :table, :value, :int end def self.down remove_column :table, :value end end Thank you for your help! -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> I want to add a column to a table. So here is my migration. My > question > is : Is it possible to say that this value must be between 1 and 7 and > must no be a float (1.4 shouldn''t be possible) ? > > class AddValueToTable < ActiveRecord::Migration > def self.up > add_column :table, :value, :int > end > > def self.down > remove_column :table, :value > end > endWell, if you define the column as an integer you''ll never end up with 1.4 as a value. It will always get truncated to an integer. Depending on your database you can add those types of restrictions. I don''t think rails migrations has support for it directly though. You could also do it as a before_save validation in the model. You''ll probably want to do this anyway so that if a user enters "1.4" you can complain early. -philip --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
This isn''t someithing to be done inside a migration, but at your model as a validation: class Table validates_inclusion_of :value, :in => (1..7).to_i end - Maurício Linhares http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) On Wed, Feb 25, 2009 at 3:49 PM, Guillaume Loader <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hello everyone! > > I want to add a column to a table. So here is my migration. My question > is : Is it possible to say that this value must be between 1 and 7 and > must no be a float (1.4 shouldn''t be possible) ? > > class AddValueToTable < ActiveRecord::Migration > def self.up > add_column :table, :value, :int > end > > def self.down > remove_column :table, :value > end > end > > > Thank you for your help! > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 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 -~----------~----~----~----~------~----~------~--~---
Ops, it''s a to_a class Table validates_inclusion_of :value, :in => (1..7).to_a end - Maurício Linhares http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) On Wed, Feb 25, 2009 at 3:54 PM, Maurício Linhares <mauricio.linhares-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This isn''t someithing to be done inside a migration, but at your model > as a validation: > > class Table > validates_inclusion_of :value, :in => (1..7).to_i > end > > > - > Maurício Linhares > http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) > > > > On Wed, Feb 25, 2009 at 3:49 PM, Guillaume Loader > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> Hello everyone! >> >> I want to add a column to a table. So here is my migration. My question >> is : Is it possible to say that this value must be between 1 and 7 and >> must no be a float (1.4 shouldn''t be possible) ? >> >> class AddValueToTable < ActiveRecord::Migration >> def self.up >> add_column :table, :value, :int >> end >> >> def self.down >> remove_column :table, :value >> end >> end >> >> >> Thank you for your help! >> -- >> 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 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 -~----------~----~----~----~------~----~------~--~---
I''m using mySQL. Can I add a parameter :min and :max? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ok so I''''ll dot it at my model. Thank you! -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Maurício Linhares wrote:> Ops, it''s a to_aWhy do we need to_a? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 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 -~----------~----~----~----~------~----~------~--~---
''Cos: (1..7).include?( 2.2 ) is true, but (1..7).to_a.include?( 2.2 ) isn''t. - Maurício Linhares http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) On Wed, Feb 25, 2009 at 4:02 PM, Guillaume Loader <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Maurício Linhares wrote: >> Ops, it''s a to_a > > > Why do we need to_a? > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 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 -~----------~----~----~----~------~----~------~--~---
Add to model validates_numericality_of :value, :only_integer => true, :less_than_or_equal_to => 7, :greater_than_or_equal_to => 1 to get the inclusive [1-7] int only On Feb 25, 8:49 am, Guillaume Loader <rails-mailing-l...@andreas- s.net> wrote:> Hello everyone! > > I want to add a column to a table. So here is my migration. My question > is : Is it possible to say that this value must be between 1 and 7 and > must no be a float (1.4 shouldn''t be possible) ? > > class AddValueToTable < ActiveRecord::Migration > def self.up > add_column :table, :value, :int > end > > def self.down > remove_column :table, :value > end > end > > Thank you for your help! > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 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 -~----------~----~----~----~------~----~------~--~---