nested polymorphic associations

4 views
Skip to first unread message

Jonathan Linowes

unread,
Jun 29, 2007, 12:17:31 AM6/29/07
to make_res...@googlegroups.com
Does make_resourceful support nested polymorphic associations?

lets say I have

# -----------------
# db
create_table "comments", :force => true do |t|
t.column "commentable_id", :integer
t.column "commentable_type", :string
t.column "body", :text
end

# model
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end

# -----------------
# this works just for articles
# model
class Article < ActiveRecord::Base
has_many :comments, :as => :commentable
end

# routes
map.resources :articles do |article|
article.resources :comments, :name_prefix => "article_"
end

# controller
class CommentsController < ApplicationController
make_resourceful do
actions :all
belongs_to :article

response_for :create, :update, :destroy do
redirect_to article_path(@article)
end
end
end

# view, e.g. new.html.erb

<% form_for(:comment, :url => article_comments_path(@article)) do |f| %>


# -----------------
# Now use it polymorphically, add a "doc" too

# model
class Doc < ActiveRecord::Base
has_many :comments, :as => :commentable
end


# how do i modify it to work with both docs and articles
I've tried
belongs_to :article, :doc
or
belongs_to :commentable

Nathan Weizenbaum

unread,
Jun 29, 2007, 1:34:46 AM6/29/07
to make_res...@googlegroups.com
No, it doesn't. This is definitely something we'd like to support, but
it's not there yet. Patches would, of course, be appreciated, but if
not, we'll get to it eventually.

- Nathan

Jonathan Linowes

unread,
Jul 1, 2007, 2:29:43 PM7/1/07
to make_res...@googlegroups.com
Hi,

I've implemented a solution that's working for me.

While the authors are cogitating my changes, I've posted them on my
blog for anyone else that may need this support in the interim.

http://www.vaporbase.com/postings/make-
resourceful_and_nested_polymorphic_associations

regards,

linoj

s.ross

unread,
Jul 1, 2007, 11:48:47 PM7/1/07
to make_res...@googlegroups.com
This has been discussed before, and I've never been able to draw a
consensus from the discussion.

I'm trying to come up with a RESTful controller using m_r and that
controller has to implement search and last_name_index (finds all by
first letter of last name). To further mix things up, search will be
called via Ajax, and if the accept format is HTML, it should render a
partial, otherwise if XML, an XML representation of the data objects.
the find-by... is quite similar, however, it will be called using a GET.

The problem: GET /localhost:3000/mycontroller/last_name_index?letter=H

resolves to:

:action => 'show'
:id => 'last_name_index'
:letter => 'H'

I understand that in REST a GET to a resource typically finds one
(show) if there is an argument (something after the resource name),
or finds all if there is no argument. What I'm wondering is how
people are customizing routes to implement search in their apps.

Thanks,

--steve

Nathan Weizenbaum

unread,
Jul 2, 2007, 12:28:33 AM7/2/07
to make_res...@googlegroups.com
What I'd do is use m_r doesn't require any sort of routing; it just
relies on the routes you generate being conventional. Thus, you could
add this into routes.rb with

map.connect 'mycontroller/last_name_index', :controller =>
'mycontroller, :action => 'last_name_index'

That said, I'm not sure that's the best way to do it. What I'd do is
have something like GET /mycontroller/index?last_name_begins_with=H (or
maybe something a bit less verbose), and then have #index delegate the
searching to something else.

I do with there were a better solution. I've been toying with the idea
of integrating search into m_r, so you could do something like GET
/mycontroller/index?last_name=Smith and it would find everyone with the
last name Smith automatically. This could be extended to allow basic
pattern-matching: GET /mycontroller/index?last_name=~H*.

- Nathan

s.ross

unread,
Jul 2, 2007, 1:40:49 AM7/2/07
to make_res...@googlegroups.com
On Jul 1, 2007, at 9:28 PM, Nathan Weizenbaum wrote:

That said, I'm not sure that's the best way to do it. What I'd do is 

have something like GET /mycontroller/index?last_name_begins_with=H (or 

maybe something a bit less verbose), and then have #index delegate the 

searching to something else.


This is probably the lowest hanging fruit. However, doesn't that imply moving index out of the make_resourceful block so the code can behave differently depending on the query string?

I do with there were a better solution. I've been toying with the idea 

of integrating search into m_r, so you could do something like GET 

/mycontroller/index?last_name=Smith and it would find everyone with the 

last name Smith automatically. This could be extended to allow basic 

pattern-matching: GET /mycontroller/index?last_name=~H*.


This would be great. I wish patterns other than CRUD were addressed by REST. Searching and sorting would be pretty high on my list of common "things" one might do with a resource.

--steve

Nathan Weizenbaum

unread,
Aug 24, 2007, 10:13:00 PM8/24/07
to make_res...@googlegroups.com
Just FYI: we haven't forgotten about this, we just haven't had a chance
to go over it yet. We will soon, hopefully.

Tom Stuart

unread,
Sep 5, 2007, 12:44:34 PM9/5/07
to make_res...@googlegroups.com
On 25 Aug 2007, at 03:13, Nathan Weizenbaum wrote:
> Just FYI: we haven't forgotten about this, we just haven't had a
> chance
> to go over it yet. We will soon, hopefully.

Great. This feature is very eagerly awaited. :-)

Reply all
Reply to author
Forward
0 new messages