Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-2843

Validation not working for my Array field

    XMLWordPrintable

    Details

      Description

      I have the following model with 3 fields. 2 fields have validations and the presence validation works on the question field. It doesn't work for choices field though. It accepts blank input in my forms.

      class Poll
        include Mongoid::Document
        embeds_many :poll_responses
        accepts_nested_attributes_for :poll_responses
        
        field :question, type: String
        field :choices, type: Array, default: Array.new
        field :duration, type: Time
       
        validates :question, presence: true
        validates :choices, presence: true, 
                            length: { minimum: 2, allow_blank: false }
       
        def has_response_from?(user)
          self.poll_responses.where(user_ids: user.id).exists?
        end
       
        def send_to_all_users
          Company.all.each {|c| c.add_to_set(:poll_ids, self.id)}
        end
       
        def send_to_users_by(criteria, value)
          Company.where(criteria => value).map {|c| c.add_to_set(:poll_ids, self.id)}
        end
       
        def update_duration(end_date)
          self.update_attributes(duration: (Time.now + end_date.to_i.days))
        end
       
        def expired?
          if self.duration
            self.duration < Time.now ? true : false
          else
            nil 
          end
        end
      end 
      

      Is there something wrong with my code or is this a Mongoid issue?

        Attachments

          Activity

            People

            Assignee:
            Unassigned
            Reporter:
            gjaldon gjaldon
            Participants:
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: