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 6916021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:32:57+00:00 2026-05-27T09:32:57+00:00

I have little knowledge about SQL queries and I am new to ruby on

  • 0

I have little knowledge about SQL queries and I am new to ruby on Rails.
This is my question. I have a 2 tables Country and State. A country has many states.
I know that there has to be foreign key in State table that joins to the Country table.
So the query to get all the states in a country would be

Select * 
From State 
Where 
State. country = Country. country
And  Country.country = 'XYZ'

I need to be able to generate a form for creating a new state, which has a drop down list of countries. While creating a state the User can choose a country to which the state belongs to. This will help me retrieve all the states in a country. I know that I need to use has_many and belongs_to in Country and State Models respectively. But I fail to understand the modifications that are to be made in the controllers and views to be able to get the desired form.

  • 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-27T09:32:58+00:00Added an answer on May 27, 2026 at 9:32 am

    To fetch all states belonging to a country in ruby/rails you’d simply call the association on the country model. eg:

    @my_country = Country.find params[:country]
    @states = @my_country.states.all
    

    This is how rails associations work. You don’t need to make the SQL yourself.

    You may also like to look at all the other methods that rails provides for associations here: http://apidock.com/rails/ActiveRecord/Associations/ClassMethods

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

Sidebar

Related Questions

I have little question about how web browser retrieve webpage? I know this User
I just started out with C and have very little knowledge about performance issues
I am an iPhone App Developer and have very very little knowledge about css.I
I'm asking this question with little/no knowledge of SMS gateways and it's relevant technologies,
I am very new to Java EE/EJB, with very little knowledge about it. I
I have little knowledge of Flash but for a little Flash game I have
I have very little programming knowledge; only a fair bit in Visual Basic. How
I have little bit longer question for you - but hope answer will be
I have little doubt about string reading in C. string reading functions like gets,
I have a little problem that involves modeling a state machine. I have managed

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.