Get database filled with data only one time?

14 views
Skip to first unread message

Luca Fuhl

unread,
Feb 17, 2014, 4:38:25 PM2/17/14
to rubyonra...@googlegroups.com
Hay guys, im pretty new to rails and im working on my first little
project (well it's a page for a friend so ˆˆ) and until now i got no
problems. But no i got my first little problem and not sure how to solve
it right :s.

I have a database with a table called game_pairs where i want to store
game parings of the bundesliga (soccer league in germany). I want to
store them only one time on the setup of the page. So i thought i create
a little helper method in an extra file in lib/assets called
get_game_data.rb. I already got the data from a website, so that works.
But now i don't know how to store this data into the database since i
can't use the database model from rails. How do i save them into the
database?

if it matters:
The data have this format :
array[{hash with data}, ..., {hash with data}].
i want to create 1 db entry for each hash. The hash looks like this
{date: 12.12.12, home_team: Fc Foo, guest_team: Fc Bar}

I hope didn't forget something and you could help me solve this problem
:)

greets

Luca

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Feb 17, 2014, 4:45:40 PM2/17/14
to rubyonra...@googlegroups.com
On 17 February 2014 21:38, Luca Fuhl <li...@ruby-forum.com> wrote:
> Hay guys, im pretty new to rails and im working on my first little
> project (well it's a page for a friend so ˆˆ) and until now i got no
> problems. But no i got my first little problem and not sure how to solve
> it right :s.
>
> I have a database with a table called game_pairs where i want to store
> game parings of the bundesliga (soccer league in germany). I want to
> store them only one time on the setup of the page. So i thought i create
> a little helper method in an extra file in lib/assets called
> get_game_data.rb. I already got the data from a website, so that works.
> But now i don't know how to store this data into the database since i
> can't use the database model from rails. How do i save them into the
> database?

Why can't you use the database model from rails? That is derive the
model from ActiveRecord as normal.

Can I first check that you have worked right through a good tutorial
such as railstutorial.org which is free to use online. That will show
you the basics of rails.

Colin

Luca Fuhl

unread,
Feb 18, 2014, 11:02:24 AM2/18/14
to rubyonra...@googlegroups.com
i worked to a rails 4 essentials tutorial on lynda.com, well i don't
think that i kept all the stuff in my mind since it's a lot if you never
worked with rails before. Thanks for railstutorial.org, didn't know
about it. I will work through it.

But how does derive from ActiveRecord solve my problem? I want to
instance an object of the model i generated and that in an file that
isn't a controller, since i do this only one time.

Ganesh Ranganathan

unread,
Feb 18, 2014, 11:13:20 AM2/18/14
to rubyonra...@googlegroups.com

On Tue, Feb 18, 2014 at 9:32 PM, Luca Fuhl <li...@ruby-forum.com> wrote:
But how does derive from ActiveRecord solve my problem? I want to
instance an object of the model i generated and that in an file that
isn't a controller, since i do this only one time.

The inheritance from ActiveRecord will allow you to add new objects easily. Create a custom rake task in lib/tasks file.

Here you can create the model objects using the data from the website and call create on each model object which would save the record in the DB. You can call this rake task immediately after rake db:migrate which would populate the database.

Also, there are some gems which will create a seeds.rb file from an already populated database. Then you don't need to crate a custom rake task and call rake db:seed

Thanks,
Ganesh

Reply all
Reply to author
Forward
0 new messages