1

Using ActiveResource, Ruby on Rails, Is there a clean way to do the following:

I have house ActiveResource model and chair ActiveResource model, and both can have comments. I want to reuse the comment ActiveResource model for both.

# ActiveResource wraps HTTP POST Requests to the following
# And then parsess the responses, and then creates instances of my ActiveResource models
POST http://3rd.party.restful.api.com/admin/houses/1/comments
POST http://3rd.party.restful.api.com/admin/houses/1/chairs/3/comments

I can only think of the following:

class Comment < ActiveResource::Base
  self.site = "http://3rd.party.restful.api.com"
  self.prefix = "/admin/:prefix_path/"
end

And then doing the following:

comment = Comment.new(:text => "some text", :prefix_path => "houses/1/chairs/3")

Please stop me from doing this.

Mark Peterson
  • 570
  • 5
  • 19

1 Answers1

0

I think you're asking if you can define the routes for each while only using one model, right?

There are two options:

First, the simplest way: just define the route twice.

resources :houses do
  resources :comments
  resources :chairs do
     resources :comments
  end
end

The routes file won't care that you're telling it the comments model can be reached from two places, and it will work basically as you expect -- views will just live in the 'comments' folder.

A second, more complex way to do it, define a namespace that you want to nest under. Then you'll end up with two controllers: CommentsController and Chairs::CommentsContoller. You might also create two sets of views, but you don't have to (the second controller can simply explicitly render the first controller's views).

For a good explanation of how namespacing would work, you can see the answer where I originally learned about it.

For what it's worth, this second approach is nice, because you can make some minor tweaks in how the model is presented depending on how it's accessed, but you've still only got one model in the DB.

Good luck! I'll be happy to try and answer questions in the comments!

Community
  • 1
  • 1
Andrew
  • 42,517
  • 51
  • 181
  • 281
  • That's a nice explanation for setting up my own routes, but I'm trying to POST to an external API, through ActiveResource, that has the following restful routes. POST /admin/houses/1/comments POST /admin/houses/1/chairs/3/comments – Mark Peterson Apr 01 '11 at 05:14
  • Well, it shouldn't matter which one you hit. If what you're posting to is a rails app then as long as your post contains the information needed to build an association then it will work at any route that has a receiving controller action that will pass the POST to the comments model. Try just hitting the shallower route with your POST and see if it actually works for both. – Andrew Apr 01 '11 at 05:19
  • My routes work fine, I'm trying to find the right pattern to utilize a 3rd party's restful API. I'm using ActiveResource. – Mark Peterson Apr 01 '11 at 05:23
  • Ok, well not being familiar with the API you're trying to connect to I don't know if there's another way to go. If you really do need to post to two different paths for the same model object, the way you've proposed is probably about as clean as it will get. – Andrew Apr 01 '11 at 05:43