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:
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?
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?
Is there a better way to do what I am attempting?