0

I want to do some testing...

If there is an array, array = [a, b, c, d], I want to save it in my database (SQLite 3) and call the elements when a user types 'array' in the input area.

I'm using cloud9 for testing! In 'migrates' folder, there is a file that has this code.

class CreateFans < ActiveRecord::Migration
  def change
    create_table :fans do |t|
      t.string :content


      t.timestamps null: false
    end
  end
end

I know I have to use this page for defining variable names of database data. but where should I put on my 'array' data?

The conclusion is...

I want to know how to save and recall an array in my database!

1

1 Answer 1

1

You could convert the array into a string to store it in the database. When you pull it from the database, convert it back into an array.

myModel.myDbObject = array.join("")

Then when pulling from the database...

array[] = myModel.myDbObject.split("")

That's essentially what I had to do for one of my Ruby on Rails applications.

Or for array elements longer than just one letter

myModel.myDbObject = array.join(",")

Pulling from database

array[] = myModel.myDbObject = array.split(",")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.