0

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.

2 Answers 2

2

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 [email protected]? 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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This helped me a lot!
0

Simply specify those validations in the person record, e.g.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.