How do I setup this odd join?

17 views
Skip to first unread message

John Sanderbeck

unread,
Apr 30, 2016, 10:04:26 AM4/30/16
to rubyonra...@googlegroups.com
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.

Sorry... Really new to rails and still trying to learn the proper
syntax...

John

--
Posted via http://www.ruby-forum.com/.

Jim

unread,
May 1, 2016, 7:44:24 PM5/1/16
to Ruby on Rails: Talk
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
Reply all
Reply to author
Forward
0 new messages