Search code examples
rubydatabaseruby-on-rails-3creationrails-console

save a text from text file through rails console


like in title - I want to save a content of a text file in database (and couple other things) through rails console - how to do it?

I'm entering rails console by: rails c and then I should do

`MyItem.create! body: content_from_my_text_file, field1: '123', field2: 'abc'

but how force rails console to read from file and write the content to the database? Is there any simple solution? Please help!


Solution

  • You don't need rails to read a file. You could get all the data of a file in a string with IO.read(path).

    You should read more about it in: