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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:24:58+00:00 2026-05-15T08:24:58+00:00

There is a table questions , and a data file questions.yml . Assume there

  • 0

There is a table questions, and a data file questions.yml. Assume there is no ‘Question’ model.

‘questions.yml’ has some recodes dump from the table.

---
questions_001:
  title: ttt1
  content: ccc1
questions_002:
  title: ttt2
  content: ccc2

I want to load the data from the yml file, insert them to database. But I can’t use rake db:fixtures:load, because it will treat the content as ‘erb’ template, which is not want I want

So I want to write another rake task, to load the data manually.

I can read the records by:

File.open("#{RAILS_ROOT}/db/fixtures/#{table_name}.yml", 'r') do |file|
   YAML::load(file).each do |record|
      # how to insert the record??
   end
end

But I don’t know how to insert them.


Edit:

I have tried:

Class.new(ActiveRecord::Base).create(record)

and

class Dummy < ActiveRecord::Base {}
Dummy.create(rcord)

But nothing inserted to database

  • 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-15T08:24:59+00:00Added an answer on May 15, 2026 at 8:24 am

    Try this after loading the date from the yml file to records:

    class Question < ActiveRecord::Base
      # Question model just to import the yml file
    end
    records.each { |record| Question.create(record) }
    

    You can simply create a model just for importing. You don’t need to create the app/models/question.rb. Just write the code above in the script responsible for the importing.

    UPDATE:

    You can use the following function:

    def create_class(class_name, superclass, &block)
      klass = Class.new superclass, &block
      Object.const_set class_name, klass
    end
    

    source

    File.open("#{RAILS_ROOT}/db/fixtures/#{table_name}.yml", 'r') do |file|
      YAML::load(file).each do |record|
        model_name = table_name.singularize.camelize
        create_class(model_name, ActiveRecod::Base) do
          set_table_name table_name.to_sym
        end
        Kernel.const_get(model_name).create(record)
      end
    end
    

    To use the connection directly you can use the following:

    ActiveRecord::Base.connection.execute("YOUR SQL CODE")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is: is there a way with a DataContext/Table mapping to implement some
I have Question and QuestionTypes. In Question table there is a foreign key that
i have one question about jquery ajax().. I created a table grid and there
in a Table there is a single attribute(column).it contains numbers.Some are repeated(2 or 3
In a MySQL (5.1) database table there is data that represents: how long a
I have an js file that appends data from webmethod to a gridview. My
Need to load data from a single file with a 100,000+ records into multiple
Need to load data from a single file with a 100,000+ records into multiple
I need to get data from an Excel file to print them in a
I don't think this question has asked yet (most similar questions are about extracting

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.