简体   繁体   中英

Ruby on Rails: How to validate an object being added to an array?

I'm currently writing a rails app for managing events. An event keeps track of all of the people objects attending. Now, I want to ensure some properties of the person before adding it to the array of people in events. How can I do this?

This is the function for adding a person to an event:

def update
    @event = Event.find(params[:id])
    email = params[:event][:people][:email]
    person_array = Person.where(email: email)
    if ! person_array.empty?
      @event.people.push(person_array[0])
    end
    redirect_to @event
end

I want to check the persons age, uniqueness in the array and want to check if the person exists.

I think you should assume there's only one email per person, so the code should be

def update
    @event = Event.find(params[:id])
    email = params[:event][:people][:email]
    person = Person.find_by_email(email: email)
    if person.present?
      @event.people.push(person)
    end
    redirect_to @event
end

Now, for the validations, with person.present? we verify that the person exist in our database, and with !@event.people.exist? we verify that the person is not in our @event's collection, so it is a new record

def update
    @event = Event.find(params[:id])
    email = params[:event][:people][:email]
    person = Person.find_by_email(email: email) 
    if person.present? and person.age > 18 and ! @event.people.exist?(person.id)
      @event.people.push(person)
    end
    redirect_to @event
end

Let me tweak it a bit :)

def update
    @event = Event.find(params[:id])
    email = params[:event][:people][:email]
    person = Person.find_by_email(email: email) 
    if person.present? and person.age > 18 and ! @event.people.exist?(person.id)
      @event.people.push(person)
      redirect_to @event
    else
        flash["Error"]
        @event.errors.add(:base, "this person does not meet al requeriments, or doesn't exist")
        render :edit
    end

end

Simply specify those validations in the person record, eg

class Person < ActiveRecord::Base
  validates :age, numericality: {only_integer: true, greater_than_or_equal_to: 12, less_than_or_equal_to: 100}

  validates :email, presence: true, uniqueness: true
  ....
end

Then simply validate associated records in your events model

class Even < ActiveRecord::Base
  has_many :people, validates: true
  ....
end

However, since you are fetching the person record from the database it should already be valid. If your people relation in events should only load a subset of all valid persons then you can specify this as part of the association declarations:

class Even < ActiveRecord::Base
  has_many :people, -> { where(some_condition: true) }
  ....
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM