andrewdmason-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Feb-21 04:18 UTC
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Tyler MacDonald
2007-Feb-21 04:45 UTC
Re: models that reference models that occur later in the migration
andrewdmason-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <andrewdmason-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---