Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 321597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:51:37+00:00 2026-05-12T08:51:37+00:00

I am playing with making a blog. I would like to have several types

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:51:37+00:00Added an answer on May 12, 2026 at 8:51 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are making a game with a playing character that will have several different
I am making a simple music playing online app. I would like to make
I'm making a game and would like to have a background loop of game
Im making a basic jQuery slider for class. and would like to have it
I am making a site which will have music playing on the site using
I was playing around making a simple haiku site using sqlalchemy and pylons. It
I am making a game that needs a crosshair. I have been playing with
I have been playing around with various methods of making the Visitor pattern in
I am making a website for my friends band. I would like to know
I am making the radio app.I have using the android media for playing radio

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.