On Sep 15, 6:15 pm, Dave Howell <[email protected]> wrote:
> class Component < Sequel::Model
> plugin :validation_helpers
>
> def validate
> super
> validates_presence [:batch_id, :ingredient_id, :percentage]
> validates_numeric :percentage
> end
> end
>
> >> Component.first.values
> => {:percentage=>#<BigDecimal:101f6bbf0,'0.1E3',4(12)>,
> :name=>"Other", :batch_id=>"350b730a-2384-11df-9419-00065b3f562c",
> :ingredient_id=>"4ace354c-2357-11df-8826-00065b3f562c"}
>
> >> c = Component.new
> => #<Component @values={}>
> >> c.ingredient_id = "4ace430c-2357-11df-9ce0-00065b3f562c"
> . . .
> >> c.valid?
> => false
>
> So far so good. . . .
>
> >> c.batch_id="375a4e3c-5fc1-11df-94f7-00065b3f562c"
> => "375a4e3c-5fc1-11df-94f7-00065b3f562c"
> >> c.valid?
> => true
>
> What? But .percentage isn't even defined yet . . .
>
> >> c
> => #<Component
> @values={:batch_id=>"375a4e3c-5fc1-11df-94f7-00065b3f562c",
> :ingredient_id=>"4ace430c-2357-11df-9ce0-00065b3f562c"}>
> >> c.percentage = 50
> => 50
> >> c.valid?
> => true
> >> c.ingredient_id = nil
> => nil
> >> c.valid?
> => false
>
> I cannot figure out why the validation system is correctly monitoring the two
> _id parameters, but completely ignoring the percentage.
I can't replicate this:
$ ruby18 -I lib bin/sequel -E sqlite:/
I, [2010-09-16T08:59:19.366805 #9252] INFO -- : (0.000135s) PRAGMA
foreign_keys = 1
Your database is stored in DB...
irb(main):001:0> DB.create_table(:components) do
irb(main):002:1* primary_key :id
irb(main):003:1> String :batch_id
irb(main):004:1> String :ingredient_id
irb(main):005:1> String :name
irb(main):006:1> BigDecimal :percentage
irb(main):007:1> end
I, [2010-09-16T08:59:20.011486 #9252] INFO -- : (0.001624s) CREATE
TABLE `components` (`id` integer PRIMARY KEY AUTOINCREMENT, `batch_id`
varchar(255), `ingredient_id` varchar(255), `name` varchar(255),
`percentage` numeric)
=> nil
irb(main):008:0>
irb(main):009:0* class Component < Sequel::Model
irb(main):010:1> plugin :validation_helpers
irb(main):011:1>
irb(main):012:1* def validate
irb(main):013:2> super
irb(main):014:2> validates_presence
[:batch_id, :ingredient_id, :percentage]
irb(main):015:2> validates_numeric :percentage
irb(main):016:2> end
irb(main):017:1> end
I, [2010-09-16T08:59:20.024988 #9252] INFO -- : (0.001320s) PRAGMA
table_info('components')
=> nil
irb(main):018:0>
irb(main):019:0* c = Component.new
=> #<Component @values={}>
irb(main):020:0> c.ingredient_id =
"4ace430c-2357-11df-9ce0-00065b3f562c"
=> "4ace430c-2357-11df-9ce0-00065b3f562c"
irb(main):021:0> c.valid?
=> false
irb(main):022:0> c.batch_id="375a4e3c-5fc1-11df-94f7-00065b3f562c"
=> "375a4e3c-5fc1-11df-94f7-00065b3f562c"
irb(main):023:0> c.valid?
=> false
irb(main):024:0> c.errors
=> {:percentage=>["is not present", "is not a number"]}
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en.