Note that there are some explanatory texts on larger screens.

plurals
  1. POForeign Keys and Multi Model forms in Rails 2.3 - Bug or feature?
    text
    copied!<p>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).</p> <p>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.</p> <p>I set up my app according to <a href="http://stream.btucker.org/post/93887650/accepts-nested-attributes-for-fields-for" rel="nofollow noreferrer">this great tutorial</a> for the new Multi model form stuff in 2.3. </p> <pre><code>class Link &lt; ActiveRecord::Base has_one :entry accepts_nested_attributes_for :entry attr_accessible :url, :description, :title, :entry_attributes end class Entry &lt; ActiveRecord::Base belongs_to :link, :dependent =&gt; :destroy #adding more types later end </code></pre> <p>From Links_Controller. Creating a new link type entry:</p> <pre><code>def new @link = Link.new() @link.build_entry respond_to do |format| format.html # new.html.erb format.xml { render :xml =&gt; @link } end end </code></pre> <p>And the view form:</p> <pre><code>&lt;% form_for(@link) do |f| %&gt; &lt;%= f.error_messages %&gt; &lt;p&gt; &lt;%= f.label :title %&gt;&lt;br /&gt; &lt;%= f.text_field :title %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.label :url %&gt;&lt;br /&gt; &lt;%= f.text_field :url %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.label :description %&gt;&lt;br /&gt; &lt;%= f.text_field :description %&gt; &lt;/p&gt; &lt;% f.fields_for :entry do |e| %&gt; &lt;%= e.label :teaser %&gt;&lt;br /&gt; &lt;%= e.text_field :teaser %&gt; &lt;%= e.text_field(:controller_name, :value =&gt; params[:controller].to_s) %&gt; &lt;% end %&gt; &lt;p&gt; &lt;%= f.submit 'Create' %&gt; </code></pre> <p>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:</p> <pre><code>mysql&gt; 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&gt; 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 | +----+-----------------+------------------------+---------------------+--------- ------------+ </code></pre> <p>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:</p> <ol> <li><p>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? </p></li> <li><p>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?</p></li> <li><p>Is there a better way to do what I am attempting?</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload