Another CTI question


#1

I’ve asked some CTI questions before and I have another one based on
my migration progress.

These are two condensed tables (the actuals being bigger):

Content
±------------+
| id |
| title |
| description |
| etc… |
±------------+

ContentJob
±-------------+
| content_id |
| location |
| date_expired |
| etc… |
±-------------+

The above is legacy stuff and I can’t really change it right now as
it’s live; my goal is to get Rails to use the existing infrastructure
and then later make changes. My AR object is below:

class ContentJob < ActiveRecord::Base
set_table_name “content_job”
set_primary_key “content_id”
has_one :content, :dependent => true, :foreign_key => “id”
end

This works well. My tests allow me to find and update fine (working
on delete right now). I have noticed tho if I create a ContentJob
object via job = ContentJob.new , assign some values and then try to
save it, I get an error saying that the primary / foreign key
(content_id) does not have a value (or something similar). Which is
fine as I understand why this is: a Content object does not exist yet.

So here’s my question:

If AR will handle the find and update logic correctly for an
existing ContentJob/Content object but not for a new ContentJob
object, could I then just create a save method in ContentJob that
will be responsible for creating the dependent Content object and
then hooking it up to the current ContentJob object? Like so:

ContentJob

def save
@content.save
self.save
end

While this is not idea since it involves a few extra lines of code,
it would be acceptable as I only have CTI on a few objects. I’m still
working on all the finer points of Rails and Ruby, so my save method
might not be completely correct and I’m assuming that when I have
created a new ContentJob object that I automagically get a new
Content object as well (not sure about this…). Anyway, does this
make sense? I have to make the CTI stuff work and STI is not in the
cards for me so hopefully the above approach will bridge this gap for
now.

tia,

  • jason