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

  • SEARCH
  • Home
  • 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 9014209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:27:16+00:00 2026-06-16T03:27:16+00:00

Here is the problem that I have been trying to solve, but I haven’t

  • 0

Here is the problem that I have been trying to solve, but I haven’t found a way that doesn’t feel like an outright hack. I have 2 objects, Warehouse and StateCity. The Warehouse object has a foreign key to a StateCity object ( state_city_id ).

Now when a user creates a Warehouse, I would like them to be able to input the State and City. Currently they can do so, and I just check the submitted params for the State and City and then create or find the corresponding ActiveRecord object. Now when they go to view the Warehouse in my view I’ve oscillated between using a helper to extract the State/City from the StateCity object if it exists and adding a method on my model to perform the same function like below

def show_state( warehouse )
  if warehouse.state_city.nil? == false
    return warehouse.state_city.state
  end
  return ""
end

def show_city( warehouse )
  if warehouse.state_city.nil? == false
    return warehouse.state_city.city
  end
  return ""
 end

Unfortunately, neither the helper method or fattening up the model with extra functions seems very natural as I have to use one for the creation of the object and another for the vieiwng of the object.

I was wondering if anyone has any advice on a better way to solve the Warehouse-StateCity problem. Any help is greatly appreciated.

  • 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-06-16T03:27:17+00:00Added an answer on June 16, 2026 at 3:27 am

    First off you could write and if statement of this kind like so:

    return warehouse.state_city.city unless warehouse.state_city.nil?
    

    but I’m sure that’s not the answer you are looking for.

    I’m assuming that you always want a Warehouse to be associated with a StateCity. If that is the case then your user always needs to select the state and city whenever a warehouse is created and that logic is handled by your controller and validated by your Warehouse model.

    If all your validations are correct then all you need to do in your show action of your Warehouse model is fetch the StateCity object from your warehouse:

    @state_city = warehouse.state_city
    

    And then in your view just access the fields @state_city.state and @state_city.city. I’m not entirely sure how you are creating these StateCity objects but that sounds pretty much how you want to do things.

    If your state_city is nil you could handle it different ways depending on what you want displayed or not. If you still want to display the warehouse show view then in the view you could do a check like

    <% if @state_city %>
      # Do something with the state city like display the state and city?
    <% end %>
    

    If you want to tell the user to update the state city for example you’d handle that from the controller by doing

    unless @state_city
      redirect_to "your edit action" and return
    end
    

    what that will do is it’ll check if your state_city is null then it’ll redirect the user to whatever action you want.

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

Sidebar

Related Questions

I have been trying to solve this problem for hours (searched here as well
I've been trying to solve a slow trigger problem and now that I have
Updated Problem solved, I have some design problem here. The directory looks like that:
I have been trying to solve this problem the whole day: How do I
I'm having a strange problem, that I've been trying to solve for too long.
I have been trying to solve this problem for a while now and I
I have been trying to solve this on my own but can't find a
I'm still a beginner and I have been trying to solve this problem by
I have a tough time trying to solve this problem. I have been at
I have been trying to solve a constrained optimization problem in R using constrOptim()

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.