Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Noob question about object arrays


  • Please log in to reply
11 replies to this topic

#1 priorcode

priorcode

    Signalman

  • Members
  • 19 posts
  • LocationCalifornia

Posted 15 August 2013 - 04:56 PM

Hello everyone,

 

I hope this is not too much of a newbie question. I have been looking for posts and haven't found anything yet, so I thought I would try the forum for the first time.

 

I am a developer at a school district creating a Rails app for scheduling some Intervention classes. I am trying to create a drop-down list of students who are not yet enrolled by subtracting an array of enrolled student numbers from an array of all student numbers. Something like this:

 

available_students = all_students - enrolled_students

 

I am getting all_students as the result, and I am assuming it is because the arrays are of different types. The all_students array looks like this in the console:  

 

[#<Student student_number: 99754.0>, #<Student student_number: 91758.0>, #<Student student_number: 92757.0>  . . .]

 

while the enrolled_students came from a different table and looks like this in the console:

 

[#<Enrollment student_number: 99754.0>,#<Enrollment student_number: 99754.0>,#<Enrollment student_number: 99754.0>. . .]

 

I am thinking that I need to convert these into simple arrays so I can subtract one from the other, but I haven't found how to do that.

 

I would really appreciate your help letting me know if I am on the right track and how this can be done.

 

 



#2 Jemagee

Jemagee

    Inspector

  • Members
  • 61 posts

Posted 15 August 2013 - 04:59 PM

Why aren't you just querying against the 'students' model for those who are not enrolled?


  • james likes this

#3 priorcode

priorcode

    Signalman

  • Members
  • 19 posts
  • LocationCalifornia

Posted 15 August 2013 - 05:24 PM

Because everything about enrollments is in the enrollments table, not the students table. 



#4 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 15 August 2013 - 05:35 PM

Why aren't you just querying against the 'students' model for those who are not enrolled?

 

Because the data is coming from 2 different tables!

 

 

You can pass an array of id's to a where clause

 

An example from http://blog.remarkab...untdown-to-2013

Article.where.not(title: ['Rails 3', 'Rails 5'])
# >> SELECT "articles".* FROM "articles" WHERE ("articles"."title" NOT IN ('Rails 3', 'Rails 5'))

Applying this to your example

Pass the array of id's from the enrolled table into the where clause for the students table so you end up with something like this

Student.where.not(id: enrolled_students)

But for that to work you need a simple array of just the ID numbers NOT inluding the

Enrollment student_number:

Not sure how best to advise on doing that as I don;t know how you got that array in the first place but hopefully that will get you in the right direction


Programming is just about problem solving!


#5 Jemagee

Jemagee

    Inspector

  • Members
  • 61 posts

Posted 15 August 2013 - 05:36 PM

Because everything about enrollments is in the enrollments table, not the students table. 

But there should be a relationship between the students and the enrollments table isn't there?  Can you show us the students and enrollments models?  I'm going off the cuff since I don't know your system but each enrollment should belong to a student and a student has_many enrollments?

 

I don't know if 'array subtraction' works in ruby but a solution would be to loop through the all students array, check if they are in the enrolled array and if they aren't push them to the available_students array.

 

all_students.each do |student|

  available_student.push(student) unless student is already in the enrolled student array

end



#6 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 15 August 2013 - 05:40 PM   Best Answer

Got it!

enrolled = Enrollment.all.collect(&:id)
Student.where.not(id: enrolled)

  • Jamie likes this

Programming is just about problem solving!


#7 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 15 August 2013 - 05:47 PM

Jemagee has a point. Your database doesn't seem to be modelled properly

 

A student should have an enrolled flag rather than a seperate enrolled table. This is a case for de-normalisation.

 

If you want to have a sperate table then set up a relationship between the two. i.e. include a foreign key from the students table in the enrolled table.

 

Maybe this is what you already have?


Programming is just about problem solving!


#8 priorcode

priorcode

    Signalman

  • Members
  • 19 posts
  • LocationCalifornia

Posted 15 August 2013 - 05:49 PM

Thanks, james, you got it! Much appreciated. 



#9 Jemagee

Jemagee

    Inspector

  • Members
  • 61 posts

Posted 15 August 2013 - 05:50 PM

Jemagee has a point. Your database doesn't seem to be modelled properly

 

A student should have an enrolled flag rather than a seperate enrolled table. This is a case for de-normalisation.

 

If you want to have a sperate table then set up a relationship between the two. i.e. include a foreign key from the students table in the enrolled table.

 

Maybe this is what you already have?

Well - that all depends on if we are only talking about intervention courses or all courses - if you're designing an enrollment model for a school your going to have a students table and courses table and an enrollment 'cross join' (that's the SQL term for it) table that contains the course ID and student ID :)

 

Have I mentioned that I love data - i love normalizing it - i love querying against it (i still remember my most favorite query that queried the same table 3 times with 2 aliases to work properly - loved seeing it work) so if any one has any normalization questions - i'd be happy to help :)



#10 priorcode

priorcode

    Signalman

  • Members
  • 19 posts
  • LocationCalifornia

Posted 15 August 2013 - 05:59 PM

Thanks for your help Jemagee,

 

Just to clarify, I do have multiple tables set up (in a similar way to our school information system) much the way you have described. I think I wasn't very clear in my first question and description of the problem. The problem I was having was in not knowing how to properly use the .map or .collect, which james has clarified in his answer. I think this will get me going. If not, I'll be back with more questions....



#11 noz

noz

    Signalman

  • Members
  • 21 posts

Posted 15 August 2013 - 08:03 PM

i love normalizing it

Why do you do that to yourself?



#12 Jemagee

Jemagee

    Inspector

  • Members
  • 61 posts

Posted 15 August 2013 - 08:04 PM

Why do you do that to yourself?

Good question - turns out i love databases - even before I knew what 'relational databases' were with my commodore 64 and then apple 2gs i was building databases to track my comic book collection (but I was doing it improperly it turns out) Databases are fun, making data my bitch gives me great joy - got some data you don't wanna futz with but need to - send it my way :)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users