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