1

So here is my non-database model.

class UserApplication::CoApplicant

  include ActiveModel::Validations
  include ActiveModel::Conversion

  attr_accessor :applicant, :first_name, :last_name, :email



  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}", value)
    end
  end

  def persisted?
    false
  end
end

I am looking to pass in an array of objects, that look something like this.

"applicant"=>{"0"=>{"email"=>"[email protected]", "last_name"=>"Jackson", "first_name"=>"Shaun"}, "1"=>{"email"=>"[email protected]", "last_name"=>"Davis", "first_name"=>"Dave"}}

The issue I am having is coming up with a way to validate each object. These values are

coming from a dynamically generated table, that can range from 1 - 10 rows.

"0" and "1" - Represents the table row. And I am looking to validate each set

of attributes { email, first_name, last_name}.

I have never had to do this type of validation before, so any help would be appreciated!

1 Answer 1

2

It looks like you just need to instantiate each of your instances and then validate them. This should be as simple as creating them based on your source array:

applicant_data = applicants['applicant']
applicant_data.each do |id, attributes|
  applicant = applicant.new(attributes)
  applicant.valid?
end

You also have a mistake in your assignment where you're calling the "#{name}" method, not the #{name}= method:

def initialize(attributes = {})
  attributes.each do |name, value|
    send("#{name}=", value)
  end
end
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot man, that was very helpful. Not sure why I was having such a hard time with this, but you cleared it right up.
I ran into this issue before on a few of my solutions, as well as the one that you have provided. "UserApplication is not missing constant Applicant!" From what I read from other sources, this is a rails < 3.1 bug? Have you experienced this, or know anything about it?
Have a look around here: example

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.