Forum: Ruby on Rails after_create not creating the right object

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
pjnet (Guest)
on 2007-02-24 17:04
(Received via mailing list)
I have 2 tables... club and forum, which use the "type" as I'm
subclassing the Club and Forum objects. Tables are:

clubs:
id
title
type

forums:
id
club_id
title
type

I've put an "after_create" callback in the Club as Club has_one Forum:

class Club < ActiveRecord::Base
  after_create :create_forum
  has_one :forum

  def self.create_forum
    # create it (taken from console)
    @f = Forum.new(:title => 'Forum', :club_id => self.id)
    @f.save
    @f.type = 'Forum'
    @f.save
  end
end

Now I know this is a bit "longhand", but if I do it from the console
as it is there, I end up with the right data in the table e.g. title,
club_id and type are correct.  But if I create it via Club.create, it
doesn't put in the type at all, thus making the application fail.

Success should put a record in the clubs table and a record in the
forums table with the correct type (Club and Forum).  What I end up
with is a Club in the clubs table and a default type in the Forum
table.

Very confused.  This should be working, and isn't!

Any help would be very much appreciated.

Paul
This topic is locked and can not be replied to.