Topic: Database/Data relation mapping

Okay, I'm new to rails, but I'm starting a project. I have a question.

Okay, lets say I have a database of people. Each row is a different person with columns such as name, address, etc. Now, there's also a column for spouse, so this column needs to hold the id of another person from the same table. How would I implement a drop down list of the "name" column which would enter the id of the spouse? Assume I'm just working from a basic scaffold (because I am right now). Also, what would be the proper naming convention for the "spouse" column in the database?

Last edited by PhogHawk (2006-07-09 09:14:56)

Re: Database/Data relation mapping

The way rails does its magic with column names and relations doesn't really work well with a table relating to itself, so what I would do is create a spouse_id column in your people table and specify the relation like this:

class Person < ActiveRecord::Base
  belongs_to :spouse, :class_name => 'Person', :foreign_key => 'spouse_id'
  # ...
end

And then in your _form.rhtml partial, you can add a spouse selection like this:
<p>
  <label for="person_spouse_id">Spouse:</label><br />
  <%= collection_select :person, :spouse_id, (Person.find_all - [@person]), :id, :name %>
</p>

I haven't tested it, but I think that will work.

Railscasts - Free Ruby on Rails Screencasts

Re: Database/Data relation mapping

Thanks a lot. This worked more than sufficiently. I just have one more question, how do I get the spouse_id column to show up in the show.rhtml view? For some reason it seems like it's not recognizing the new column in the Person.content_column method that's used in the loop for show.rhtml.

Edit: after looking through the code, I realize that Rails probably doesn't recognize any column name with "id" in it as a "content_column". That's probably why it's not showing "spouse_id". I'm still curious how to display each person's spouse in the table in show.rhtml. Can anyone help?

Last edited by PhogHawk (2006-07-09 17:17:23)

Re: Database/Data relation mapping

PhogHawk wrote:

after looking through the code, I realize that Rails probably doesn't recognize any column name with "id" in it as a "content_column". That's probably why it's not showing "spouse_id". I'm still curious how to display each person's spouse in the table in show.rhtml. Can anyone help?

Yeah, it just doesn't display relationships when it loops through the columns. You can do something like this to display the spouse:

<p>
  <strong>Spouse:</strong> <%=h @person.spouse.name %>
</p>

Removing the column loop is usually one of the first things I do after generating the scaffold because I want to customize the information that is displayed.

Railscasts - Free Ruby on Rails Screencasts

Re: Database/Data relation mapping

This isn't the most useful comment in the world, but there is some plugin somewhere that adds associations to the default scaffolder.

You can also try looking at Streamlined; I think that does associations, too.

Evan