0

I want to pull every single student first name and last name from my job's database; so that I can keep updating my local database with new students. The issue is that the students' first names and last names are in different rows; therefore, when I try to save these students, I also get two different rows.

I have tried creating a student with Student.new(student_parameters) and then setting up two variables (one for the first name and the and other one for the last name) every time this information becomes available as the program rolls over every single piece of information.

After that, I have tried saving the student and then updating that same student; but it creates another row when I update it.

This is what I have as of now:

NewOcOrderOption.where("name = 'Student First Name' OR name = 'Student Last Name'").each do |key|
          #binding.pry

          @st = Student.new(student_params)

          @st.id = key.order_id

          @st.first_name = key.value if key.name == "Student First Name"

          @st.save!

          stu = Student.find(@st.id)

          @st.last_name = key.value if key.name == "Student Last Name"

          stu.update_attribute(:last_name, @st.last_name) if @st.last_name != nil

          #@st.save!

        end

If I am not explaining myself well, I deeply apologize. I have been tackling this for 5 hours now. Thank you.

Also, this is all taking place at my student's controller.

Edit:

The original database looks like this

This is how I want it to look like:

id first_name last_name 4074 Cristian Polanco 4075 Raul Person

This is the output:

id first_name last_name 4074 Cristian NULL 4074 NULL Polanco 4075 Raul NULL 4075 NULL Person

  • if i understand well every `NewOcOrderOption` instance contains either the firstname or the lastname of one student. But what is the common point between two instances of option ? order_id ? I assume you don't want to attribute one firstname and one lastname randomly – Sovalina Jun 04 '18 at 22:49
  • @sovalina, the `NewOcOrderOption` instance contains the first name and last name of the students, as well as the parent's information. Each one of the rows is identified by an `order_id`, which I want to be the id for each students created. In the future, the id of the student will serve me to create a validation of whether that student exists or not. The `NewOcOrderOption` table looks like [this](https://imgur.com/1YOUusJ) – Cristian Polanco Jun 05 '18 at 14:14

1 Answers1

0

You can use group_by on NewOcOrderOption records.
It will return an hash with order_id as key and all the options (for this id) as value:

NewOcOrderOption.all.group_by(&:order_id).each do |id, options|
  student = Student.new
  options.each do |option|
    case option.name
    when 'Student First Name' then student.firstname = option.value
    when 'Student Last Name' then student.lastname = option.value
    end
  end
  student.id = id
  student.save!
end

However, note that id is attr_protected and you should add another attribute to the Student model to store the order_id instead of override the id primary key (you can check this post for more explaination)

Sovalina
  • 5,410
  • 4
  • 22
  • 39
  • Thanks a lot, this solved my issue. Is amazing how quickly this issue was solved, yet after 5 hours I was going crazy yesterday. I will look into the post, again thanks a lot I really appreciate the help! – Cristian Polanco Jun 05 '18 at 16:14