Also, I'd guess that the categories.include? (id) is not what you want 
since it's an actual list of category models and a literal reading of the 
value passed would suggest it's comparing against hash { :id => 1 }, which 
would be false.  Perhaps category_ids.include? would be more to what you're 
after.

On Friday, November 23, 2012 12:03:19 PM UTC-7, Bryan Larsen wrote:
>
> The if clause does conditional validation:  the validation only runs 
> when the if clause is true; when the if clause is false the validation 
> isn't even checked.  I don't think that's what you want. 
>
> You're probably best off with validates_each: 
>
>
> http://guides.rubyonrails.org/active_record_validations_callbacks.html#validates_each
>  
>
> You can then put your conditions inside the block and it should work 
> as you expect. 
>
> Bryan 
>
>
> On Thu, Nov 22, 2012 at 7:57 AM, tonym <[email protected] <javascript:>> 
> wrote: 
> > Hi all, 
> > 
> > not specifically a Hobo thing, but I can't find a resolution on other 
> > forums... 
> > I'm trying to validate that a field is not null, if a specific category 
> has 
> > been picked from the has_many association. 
> > Model speaks for itself (this is simplified, not all my code)... 
> > 
> >   fields do 
> >    food_range :text 
> >    timestamps 
> >   end 
> > 
> >   has_many :categories, :through => :category_assignments, :accessible 
> => 
> > true 
> >   has_many :category_assignments, :dependent => :destroy 
> > 
> >   validates :food_style, :presence => true, :if => 
> > "self.categories.include?(:id => 1)" 
> > 
> > All I'm trying to do is check if :id => 1 is included in the list of 
> > categories.  This always returns false at the moment. 
> > 
> > Any ideas? 
> > 
> > Cheers, Anthony. 
> > 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups 
> > "Hobo Users" group. 
> > To view this discussion on the web visit 
> > https://groups.google.com/d/msg/hobousers/-/MnwmPW_gHD8J. 
> > To post to this group, send email to [email protected]<javascript:>. 
>
> > To unsubscribe from this group, send email to 
> > [email protected] <javascript:>. 
> > For more options, visit this group at 
> > http://groups.google.com/group/hobousers?hl=en. 
>

-- 
You received this message because you are subscribed to the Google Groups "Hobo 
Users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/hobousers/-/A3avIsTkTwoJ.
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/hobousers?hl=en.

Reply via email to