views:

377

answers:

1

I am playing with making a blog. I would like to have several types of entries (a link to an interesting site + brief comment, a traditional blog post with title and body text, a picture... you get the idea).

The idea seemed straight forward. An entry table/model with a the few details common to all those types (creation time and a little teaser/preview text or something), and then a table/model for each type of entry I would like that will reference the entry table/model.

I set up my app according to this great tutorial for the new Multi model form stuff in 2.3.

class Link < ActiveRecord::Base
  has_one :entry
  accepts_nested_attributes_for :entry
  attr_accessible :url, :description, :title, :entry_attributes

end

class Entry < ActiveRecord::Base
  belongs_to :link, :dependent => :destroy #adding more types later

end

From Links_Controller. Creating a new link type entry:

def new
    @link = Link.new()
    @link.build_entry

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @link }
    end
  end

And the view form:

<% form_for(@link) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.label :url %><br />
    <%= f.text_field :url %>
  </p>
  <p>
    <%= f.label :description %><br />
    <%= f.text_field :description %>
  </p>
  <% f.fields_for :entry do |e| %>
      <%= e.label :teaser %><br />
      <%= e.text_field :teaser %>
      <%= e.text_field(:controller_name, :value => params[:controller].to_s) %>
      <% end %>
  <p>
    <%= f.submit 'Create' %>

I am storing the controller name just so I can list entries but refer to the proper controller to display. Although it appears to work (the multi model form is submitted and a row appears in the table for each of the models involved) the foreign key field is null for all the entries. In the database I get:

mysql> select * from links;
+----+------------+------------------+---------------------+-------------+
| id | entries_id | title            | url                 | description |
+----+------------+------------------+---------------------+-------------+
| 10 |       NULL | Snazzy website   | www.somewebsite.com | Cool site   |
| 11 |       NULL | Snazzy website   | www.somewebsite.com | Cool site   |
| 12 |       NULL | Snazzy website   | www.somewebsite.com | Cool site   |
| 13 |       NULL | Snazzy website 2 | www.ab21e312e3c.com | Description |
| 14 |       NULL | fk_test          | fk_test             | fk_test     |

mysql> select * from entries;
+----+-----------------+------------------------+---------------------+---------
------------+
| id | controller_name | teaser                 | created_at          | updated_
at          |
+----+-----------------+------------------------+---------------------+---------
------------+
| 10 | links           | Check it out           | 2009-08-11 09:06:47 | 2009-08-
11 09:06:47 |
| 11 | links           | Check it out           | 2009-08-11 09:08:49 | 2009-08-
11 09:08:49 |
| 12 | links           | Check it out           | 2009-08-11 09:09:04 | 2009-08-
11 09:09:04 |
| 13 | links           | This is interesting... | 2009-08-11 09:27:29 | 2009-08-
11 09:27:29 |
| 14 | links           | fk_test                | 2009-08-11 20:42:26 | 2009-08-
11 20:42:26 |
+----+-----------------+------------------------+---------------------+---------
------------+

This lack of foreign keys is making retrieval difficult, since it usually relies on the FK values. A couple of questions come out of this:

  1. I know DHH writes off DB constraints as business logic that conceptually belongs in the model, so is this how its supposed to work or did I do something wrong?

  2. Should I figure out a way to grab data based on the fact that the id is the same in both the entries table and the links table?

  3. Is there a better way to do what I am attempting?

A: 

It looks like your foreign key is in the wrong table. The model with the belongs_to association should have the foreign key, so the entries table should have a link_id column.

1) I know DHH writes off DB constraints as business logic that conceptually belongs in the model, so is this how its supposed to work or did I do something wrong?

The foreign key should be set, so it's not working right. As for whether the constraints belong in the model or database, I've heard good arguments on both sides.

2) Should I figure out a way to grab data based on the fact that the id is the same in both the entries table and the links table?

Definitely not, this is not guaranteed to stay the same. All it would take is one more row on either side to offset everything causing quite a mess.

3) Is there a better way to do what I am attempting?

Try adding the link_id column to entries and see if that works for you.

ryanb
Thanks Ryan! You were right about the FK. I had reversed the relationship. Once I fixed that the queries made more sense and revealed the true source of the problem; my tables were defined with references :entries instead of references :entry.Now everything is working!Thanks a bunch for taking the time to answer. I am a big fan of your railscasts BTW. Keep up the great work!
Mike Williamson