2

I am new to the Ruby on Rails thing, and while I like the organization and standards provided, I am a little confused on how to make rails work for me in this specific case.

I have a Webservice that I want to use my rails application with. Making a direct connection to the database would be nice and provide me instantly with the models I need to make my Rails application work.

However, I would have to replicate all of the logic provided by the webservice(which isn't trivial). If I didn't make a direct connection to the database, how would I persist the models(such as a user model).

Would I have to make a separate database that mimics the server's DB but never actually interacts with it directly?

Thanks in advance -- let me know if you need clarification.

EDIT: Example

  1. I have a rails app that gets on URL www.mywebservice:8080.com/users/5
  2. Service returns JSON {name:foo,nick:bar,friend:baz}
  3. At this point how do I tell rails to make a User object out of what it just got and then store it in the database? Or do is there a way to persist this JSON object?

2 Answers 2

5

ActiveResource handles your use case just fine http://api.rubyonrails.org/classes/ActiveResource/Base.html

What it does is reflect on the json returned by the service and fake out the object to make it look like it's a real object.

class User < ActiveResource::Base
end

user = User.find(1) 
puts user.name  
# "scott"
Sign up to request clarification or add additional context in comments.

Comments

1

Why rails if you don't need any of its features? I'd recommend start with Sinatra, then add the libraries you need, as JSON, ActiveRecord(?) that Rails ships with.

You may connect to any database you want and you don't have to use ActiveRecord, however, it's hard to understand what you're really asking. How is this title related to the question? Why direct DB? You don't want to instantiate a User-object and then do a user.to_json on it?

KISS! :)

1 Comment

1. My title relates to the question because I am asking how to mimic a REST API's database without actually connecting to the API's database(or even if I need to mimic it at all). 2. Instantiating a user-object means that I have to have a User model -- right? Doesn't that mean that I'd need to have a respective Users table?

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.