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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:46:19+00:00 2026-06-17T01:46:19+00:00

As a learning exercise, I’m trying to convert an existing Sinatra app to a

  • 0

As a learning exercise, I’m trying to convert an existing Sinatra app to a Rails app. The information in countries.txt will eventually be moved into a database, but in order to keep things simple for me, I’d like to first read data in from the text file, in the same way the source app did. Problem is I can’t figure out where File will read from in a Rails app. Where in the Rails directory do I put the countries.txt document for a method in a model to have access?

def get_random
  content = File.read("countries.txt")
  words = content.split("\n")
  words[rand(words.size)].upcase
end
  • 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-17T01:46:20+00:00Added an answer on June 17, 2026 at 1:46 am

    I don’t have a good suggestion on where to put countries.txt, but let’s say you put it in the ‘config’ directory. You could then use the following to read it, regardless of what file is doing the reading.

    content = File.read(File.join(RAILS_ROOT, 'config', 'countries.txt'))
    

    However, if you don’t want them in the database, there aren’t that many countries… I would consider creating a file in say config/initializers/countries.rb that had something like this:

    COUNTRIES = ['Country 1', 'Country2', etc...]
    

    Or a hash mapping the name to the iso code. The advantage to this is you’re only reading the file once, not every time you need to get a random country.

    But with all that said.. you could also use one of the country gems that are out there to deal with it for you.

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

Sidebar

Related Questions

For a learning exercise I converted a sinatra/backbone app to the Rails environment. I
I'm trying (as a self-learning exercise) to create a Clojure macro that will generate
As a learning exercise, I'm trying to implement a class which will emulate the
Here's the scoop: As a learning exercise, I'm trying to write a Rails clone
I'm trying to write a simple baseball scorekeeping app, mostly as a learning exercise.
I'm writing a simple project management app as a learning exercise in Rails. I
I'm developing a Python web app as a learning exercise, and I am looking
As a learning exercise, I'm trying to implement a heapsort in Haskell. I figured
Im quite new to generics and as a learning exercise Im trying to create
I am currently learning Rails and CSS, and am trying to replicate the look

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.