Eduardo Yáñez Parareda
2010-Dec-30 16:39 UTC
validates_inclusion_of doesn''t match constant
Hello, I have a model Model: SPECIAL_FEATURES = %w(none top) class Model validates_inclusion_of :special_feature, :in => Model::SPECIAL_FEATURES, :on => :create, :message => "special_feature %s is not defined" belongs_to :team def special_feature attributes = attributes_before_type_cast if attributes["special_feature"] read_attribute(:special_feature).to_sym else nil end end def special_feature=(value) write_attribute(:special_feature, value.to_s) end end In another class I do: model = Model.new model.special_feature = :none model.save! Then I got this error: Validation failed: Special feature special_feature {:model=>"Model", :attribute=>"Special feature", :value=>"none"} is not defined (ActiveRecord::RecordInvalid)! But I think it''s defined... I''ve followed some tips to work with enums, so it seems this is the way to work with them, but I''m doing something wrong, any ideas? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
2010/12/30 Eduardo Yáñez Parareda <eduardo.yanez-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> Then I got this error: > > Validation failed: Special feature special_feature > {:model=>"Model", :attribute=>"Special feature", :value=>"none"} is > not defined (ActiveRecord::RecordInvalid)!I wonder if there''s some confusion due to you assigning a symbol, but checking for strings... Try:> model = Model.new > model.special_feature = "none" > model.save!-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Eduardo Yáñez Parareda
2010-Dec-30 17:04 UTC
Re: validates_inclusion_of doesn''t match constant
Well, I''ve just tested with "none" and ''none'' and I''ve got the same error. On 30 dic, 17:43, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 2010/12/30 Eduardo Yáñez Parareda <eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > Then I got this error: > > > Validation failed: Special feature special_feature > > {:model=>"Model", :attribute=>"Special feature", :value=>"none"} is > > not defined (ActiveRecord::RecordInvalid)! > > I wonder if there''s some confusion due to you assigning a symbol, but > checking for strings... > > Try: > > > > > > > > > model = Model.new > > model.special_feature = "none" > > model.save!-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
On Dec 30, 5:04 pm, Eduardo Yáñez Parareda <eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Well, I''ve just tested with "none" and ''none'' and I''ve got the same > error. >I think symbol/string confusion is the problem, but not in that way. Your setter method coerces everything to a string to clearly it doesn''t matter if you pass a string or a symbol to that. However your getter method forces its return value to be a symbol, which your validation then tries to compare to a string. Fred -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Michael Pavling
2010-Dec-30 18:12 UTC
Re: Re: validates_inclusion_of doesn''t match constant
On 30 December 2010 18:04, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Dec 30, 5:04 pm, Eduardo Yáñez Parareda <eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> Well, I''ve just tested with "none" and ''none'' and I''ve got the same >> error. >> > I think symbol/string confusion is the problemGah! I didn''t even look at the methods... why overload them? oh well, I''m sure there''s a perfectly valid reason</cynical disbelief> So Eduardo, change the constant: SPECIAL_FEATURES = [:none, :top] ...does that do it? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Michael Pavling
2010-Dec-30 18:14 UTC
Re: Re: validates_inclusion_of doesn''t match constant
On 30 December 2010 18:12, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So Eduardo, change the constant: > SPECIAL_FEATURES = [:none, :top] > > ...does that do it? >You might also want to change the setter: def special_feature=(value) write_attribute(:special_feature, value.to_s.to_sym) end Worth a bash... or better yet, I could fire up a console and check... but I''ve gotta pack for a trip in the morning, so no time ATM -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Eduardo Yáñez Parareda
2011-Jan-03 09:43 UTC
Re: validates_inclusion_of doesn''t match constant
Thanks Michael, that was enough so it works now. I followed the tip to emulate enums from this article (http://thinkinginrails.com/2010/04/ more-on-using-enums-for-constant-data-in-rails/), but it seems to have a little mistake.> So Eduardo, change the constant: > SPECIAL_FEATURES = [:none, :top] > > ...does that do it?-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.