models that reference models that occur later in the migration

Hello,

I have a relationship definition in my model definition, campaign.rb that references another model, campaign_memberships.rb, that isn't created until a later migration.

  has_many :active_memberships,             :class_name => "CampaignMembership",             :foreign_key => "campaign_id",             :conditions => ["campaign_memberships.campaign_membership_status_id != ?", CampaignMembershipStatus.find_by_code('resigned').id]

When I run the migration from the beginning, it bombs out here. What's the best way to get around this problem?

Thanks,

Andrew

I have a relationship definition in my model definition, campaign.rb that references another model, campaign_memberships.rb, that isn't created until a later migration.

If the problem is really the order (but I'm not convinced it is, see below), change the order of the migration, so that campaign_memberships happens first. Assuming these are in the "db/migrate" directory, the way to do this is to name them like:

001campaign_memberships.rb 002campaign.rb

If you use the "scripts/generate migration" command to create your migrations, the order should be sorted out for you naturally.

  has_many :active_memberships,             :class_name => "CampaignMembership",             :foreign_key => "campaign_id",             :conditions => ["campaign_memberships.campaign_membership_status_id != ?", CampaignMembershipStatus.find_by_code('resigned').id]

Also, you shouldn't need to reference any actual model object here... the migrations should really be thought of as a lower level than that. You could make the "conditions' an actual SQL statement like

:conditions =>   "campaign_memberships.campain_membership_status_id = " +   "(SELECT id FROM campaign_membership_status WHERE code = 'resigned')"

  - Tyler