views:

26

answers:

1

Hi everyone.

SCENARIO:

Given that a model called Edition has its community feature enabled

I want all Records under that Edition to validate for the community field

When the community feature is disabled, the community field will NOT be validated

Basically, I am trying to write a custom validation function at the ActiveRecord level, that will check if the parent edition has the proper true/false value.

But I am not sure the best way to handle this. My instinct is something like this, but I though I would get the communities' feedback:

class Record < ActiveRecord::Base
validate edition_has_communities?

private
  def edition_has_communities?
    if self.edition.communities_enabled
       if community.blank?
          errors.add(:community, "must be filled out for this Edition")
       end
    end
  end
end

My concern is, that this method depends on the association with the Edition being defined prior to validation, and that may not always be the case. Would this be something that should be validated on the front end maybe?

Thoughts?

A: 

Looks like that would work just fine to me, and if you're worried about whether the edition association is defined yet, why not just add a check?
if self.edition and self.edition.communities_enabled
To me this isn't something to validate on the front end, I think you're right to put this in the model. Are there things that ever really should be validated on the front end?

bergyman
Haha Too True. :) I'll go ahead and give it a shot as is. (And your right I could just add a check.. Sometimes I swear I can't see the forest for the trees. :)
Dustin M.
It worked.. lol Thanks for the feedback.
Dustin M.
Sweet. Wasn't trying to be snippy or anything with asking whether or not anything should be validated on the front end - I really am curious as to whether people think there are things that should be. Glad it's working - sometimes all you need is another pair of eyes.
bergyman