Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 354599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:56:22+00:00 2026-05-12T11:56:22+00:00

I have a Map model, which defines the details for an ASCII-art map for

  • 0

I have a Map model, which defines the details for an ASCII-art map for a given area in a MUD. I want to be able to reach each map’s page using its name instead of its ID, so if I have a map of the Western Ithmia, I want to be able to access it from /maps/western_ithmia (but also /maps/withmia).

I have a second model, MapAlias, which contains all of the existing shortnames that can be used in a path to reach a map. A foreign key is stored with each alias, referring to the ID of its associated Map.

What I want to do is create a new MapAlias simultaneously with each newly-created Map. Unfortunately, it doesn’t seem like I can get at my new Map‘s ID until it’s been saved, but I don’t want to save it until I’ve validated/saved its new MapAlias. What makes this worse is that I want each Map to have knowledge of which MapAlias is its primary alias, i.e. the one that generated links will refer to. This seems like a circular dependency to me…

I’m still pretty new to Rails, so I’m not sure what I should be doing, but I’ve explored model callback hooks (like before_save) with only limited success – I still have the circular dependency to deal with. Any ideas? =/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:56:22+00:00Added an answer on May 12, 2026 at 11:56 am

    Okay, after a fair bit of time, I think I have a working solution. When I wrote this question, the Map has_many MapAlias’es, and each MapAlias belongs_to a Map. I recently realized that what I wanted was another association: a Map belongs_to its primary name, which the website uses in links to that map. I added that and three hooks – two before_validation_on_* for setting/creating the alias, one after_create for setting the alias’s map_id foreign key for the original associations – and it works like a charm.

    To the controller it’s just a Map.save call, but internally, the alias is saved first:

    belongs_to :shortname, :class_name => "MapAlias" # primary name
    
    before_validation_on_create :add_map_alias
    def add_map_alias
      self.create_shortname(:title => self.title)
    
      @alias_failed = !(self.shortname.errors.empty?)
    
      nil
    end
    
    before_validation_on_update :set_map_alias
    def set_map_alias
      self.shortname.title = self.title
      self.shortname.save(false)
    
      @alias_failed = !(self.shortname.errors.empty?)
    
      nil
    end
    
    after_create :set_alias_map_id
    def set_alias_map_id
      self.shortname.update_attribute :map_id, self.id
    
      nil
    end
    

    It’s probably not the most ideal solution, but it works just fine for my purposes. Thanks for everyone’s advice, at any rate!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an enum type on my Java model which I'd like to map
I have map.resources :posts and I want to be able to serve post bodies
I have a map which has got marker based on state of US. Each
I have a model on top of my database model and map the objects
I have a ViewModel I am binding to a view: ProductViewModel model = Mapper.Map<Product,
I want to display google map in Android ,i have map API even map
I have a Fieldnote model in my app, which has_many :activities attached to it
I have a simple model which is just a chain of one-to-many relationships: Country
I have the following JTable which uses a table model: http://s17.postimage.org/7zfh3l4lr/Screen_Shot_2012_03_10_at_15_11_31.png Instead of using,
I have a model object which subclasses ActiveRecord. Additionally, using STI, I have defined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.