Ruby on Rails with REST API
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 pro开发者_如何学运维vided 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
- I have a rails app that gets on URL www.mywebservice:8080.com/users/5
- Service returns JSON {name:foo,nick:bar,friend:baz}
- 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?
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"
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! :)
精彩评论