On Monday, April 30, 2018 at 3:10:19 PM UTC-7, craig buchanan wrote:
>
> The `validates_includes` validation works with strings and numeric values
>
> require 'sequel'
>
> DB = Sequel.sqlite
>
> DB.create_table(:users) do
>   primary_key :id
>   String :name, null: false
>   String :type
> end
>
> class User < Sequel::Model
>   plugin :validation_helpers
>
>   def validate
>     super
>     validates_presence [:name]
>     validates_includes %w(a b c), :type
>   end
>
> end
>
> irb> u = User.new(name: 'craig', type: 'a')
> => #<User @values={:name=>"craig",:type=>a}>
> irb> u.valid?
> => true
>
> However, if the validation is changed:
>
>   def validate
>     super
>     validates_presence [:name]
>     validates_includes %i(a b c), :type
>   end
>
> the validations fail:
>
> irb> u = User.new(name: 'craig', type: :c)
> => #<User @values={:name=>"craig",:type=>:c}>
> irb> u.valid?
> => false
> irb> u.errors
> => {:type=>["is not in range or set: [:a, :b, :c]"]}
>
> Can symbols be used in this type of validation?
>

Yes, but in general it wouldn't make sense unless type was a virtual column 
(and it isn't in your example), since you can't store the symbols in the 
database.

I think the issue you are having is that User.new is going to typecast :c 
to 'c' when it calls User#type=, since the column type of type is string.  
It's odd that your inspect output is showing :type=>:c instead of 
:type=>"c", but I'm guessing that is forged, since the previous example 
inspect output uses :type=>a instead of :type=>"a".

Thanks,
Jeremy 

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to