Ruby on Rails Sunday, May 1, 2016

On Saturday, April 30, 2016 at 10:04:26 AM UTC-4, Ruby-Forum.com User wrote:
What I am trying to do is the following:

I have a Student table with some fields...

I need to join the student in two different ways...

1. The student has a home district
2. The student can be in one or more reporting districts

I understand the join table for the reporting districts, however is it
also possible to have a district_id in the student table as well which
is his/her home district?

Or do I have to do a has many through?

I would like to be able to do

  student.district as well as student.reporting_districts or something
similar. 

You could add a district_id field to the students table and set it up as a belongs_to relationship. 

Or, you already have a has_many through table to connect students and districts (your reporting districts), correct? You could add a field in that table to indicate the primary district.

If a student will only ever have one primary district, I would generally prefer the first way.

Jim Crate

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c0d5bfbe-922f-4fd6-b9d8-dad089f90f9b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment