0

Is it possible to handle databases in Ruby as you would in a Rails website?

I'm writing a Websocket game server in pure Ruby, and it would be easier if I could just handle data in Ruby as you would in rails: User.find_by(:email) or User.save instead of using SQL statements. Is this possible, or better yet, could I write a websocket SERVER in Rails? I'm using Postgres and Ruby 2.0.0. I'm also writing a Rails application using Rails 4.

Melvin Sowah
  • 700
  • 1
  • 10
  • 29

1 Answers1

2

Is it possible to handle databases in Ruby as you would in a Rails website?

Yes, activerecord is a gem and can be used outside of Rails.

See this question/answer: https://stackoverflow.com/a/1643938/382982

You could also use DataMapper, Sequel, etc.

Community
  • 1
  • 1
pdoherty926
  • 9,895
  • 4
  • 37
  • 68
  • +1. One other thing: canI share ActiveUser objects between my website and server? For example, I create a User object from ActiveRecord on my Rails site, is there a way to reference that same object in my server, or do I have to hard code it to fit with my database? – Melvin Sowah Aug 25 '13 at 20:54
  • In that case, why not have your non-Rails site act as an API client of your Rails site? – pdoherty926 Aug 25 '13 at 21:43
  • I'm not sure I understand; by non-Rails site, you mean my server? Would that just involve having my ActiveRecord objects live with my websocket server instead of the website, then use Rails to `require` those objects? Sorry for noobish question :/ – Melvin Sowah Aug 26 '13 at 04:28
  • Yes - I meant your server. I've never worked with WebSockets and Rails, but I'd imagine you'd send your objects over the socket as JSON, make whatever changes you nedd to and then broadcast them back to the server, who'd look for an ID and update that record accordingly. – pdoherty926 Aug 26 '13 at 15:01