Nested Models? Creation Order


I am learning Rails and have run into a bit of difficulty with something
I suspect is not uncommon, but I'm having difficulty getting my point
across to Google.

I have three models in an application designed to track events in time,
one model (a 'date' model) has a polymorphic relationship to the other
two (the others are two different kinds of events). The other two models
really aren't valid without a corresponding date record. I have chosen
to use a polymorphic association over STI because, as I understand it,
STI is a less efficient approach to the same functionality that a
polymorphic association provides.
The polymorphic relationship that links a date record to one of the
event model records is configured and seems to work okay.

During the initial development process, I was performing object
instantiations and other operations for these objects using two steps,
one step to retrieve the event object, and the second step to retrieve
the date object. Creation operations (and others) were the same way, one
step to create a new event object, the next to create a new date object.
In these operations, the polymorphic association was successfully
providing the desired linkage between the relevant records.

It seemed to me that since the event models aren't valid without a date
model, that the date model should be nested in the event models and the
event models should mediate access to their respective date records.
Therefor when I load one of the event models, it quietly loads its
associated date model. When I want to access the event model's date, I
ask the event model for it, eg. when = expense.bcdate

The problem I have is that when I am creating a new event record, rails
seems intent on saving the date record before saving the event record,
(which seems to make sense to me), and thus the date record's
polymorphic _id field winds up empty at the conclusion of the save
sequence. There seeem to be a lot of different ways to approach this and
I have tried a number of different methods to solve this but they've
either seemed poorly designed or they've ended in failure. I was going
to explain the approaches I've taken but as a neophyte, I thought it
might be better to ask for an approach from the experienced rather than
directing attention to getting the junk code I've written to work.

For what it's worth, here's an abridged version of my models:

class RfBcDate < ActiveRecord::Base
  belongs_to :eventage, :polymorphic => true

class RfBcExpense < ActiveRecord::Base
  has_one :rf_bc_date, :as => :eventage

  def bcdate
    RfBcDate.first(:conditions => ["eventage_id = ? AND eventage_type =
?", id, self.class.to_s.pluralize]).bcdate

  # An exception is thrown if this isn't present during creation
  def bcdate=(new_date)
    # When called with in a nested model, the enclosing model isn't
saved yet. => new_date, :eventage_type =>
self.class.to_s.pluralize, :eventage_id => id)


Xavier Snark wrote:

I am learning Rails and have run into a bit of difficulty with something
I suspect is not uncommon, but I'm having difficulty getting my point
across to Google.
when I am creating a new event record

I think you've minced some concepts here.
states that you can access the nested model through an instance that has
a polymorphic association with another model, eg. "From an instance of
the Employee model, you can retrieve a collection of pictures:"

Polymorphic associations give you the abilities that you're after, but
not entirely. I believe that when you are creating and updating, you
need to rely on NestedAttributes of the ActiveRecord model in order to
use a single form for updating both the "subject" record and the related

I haven't successfully used this, but the documentation suggests that
you must update your "subject" model with the
'accepts_nested_attributes_for :secondary' where :secondary is the
nested model in order to manipulate the related record through the
subject record's controller. This gives you the ability to modify the
related record through values supplied in the request params hash.


Hi X,

Here's a walkthrough that illustrates what you are after...


Xavier Snark wrote:

Here's a walkthrough that illustrates what you are after...

This page displayer thngy from Google Docs doesn't seem to work
perfectly. It keeps truncating the document, so I've posted it here as