Hi! I have a problem related to polymorphic associations. Currently in my routes i define the following: map.resources :articles do |articles| articles.resources :comments end map.resources :posts do |posts| posts.resources :comments end I have basically posts and articles models which relate to the polymorphic model comment. Routes are generated as I expect to, but the problem resides in the comments_controller, since in this controller I''m not able to figure out if I''m creating a comment for a post or for an article. Is there a (possibly clean) way to solve this? Thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Serafino Picozzi wrote:> Hi! > > I have a problem related to polymorphic associations. > Currently in my routes i define the following: > > map.resources :articles do |articles| > articles.resources :comments > end > > map.resources :posts do |posts| > posts.resources :comments > end >try this map.resources :articles ,:has_many=>:comments map.resources :posts ,:has_many=>:comments -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Pokkai Dokkai wrote:> try this > map.resources :articles ,:has_many=>:comments > map.resources :posts ,:has_many=>:commentsThis will only shorten the code in routes, but not solve my problem. Thanks for the tip in any case -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Oct 14, 4:40 am, Serafino Picozzi <rails-mailing-l...@andreas- s.net> wrote:> Hi! > > I have a problem related to polymorphic associations. > Currently in my routes i define the following: > > map.resources :articles do |articles| > articles.resources :comments > end > > map.resources :posts do |posts| > posts.resources :comments > end > > I have basically posts and articles models which relate to the > polymorphic model comment. > Routes are generated as I expect to, but the problem resides in the > comments_controller, since in this controller I''m not able to figure out > if I''m creating a comment for a post or for an article. > > Is there a (possibly clean) way to solve this?You can check for the presence of params[:post_id] or params[:article_id] to discover the route that got triggered. I generally use a before_filter: class CommentsController < ApplicationController before_filter :determine_parent before_filter :ensure_valid_comment def determine_parent @parent = Article.find_by_id(params[:article_id]) || Post.find(params[:post_id] end def ensure_valid_comment return unless params[:id] raise ActiveRecord::RecordNotFound unless @parent.comments.find(params[:id]) end end This will first look for an article, and if that fails (returns nil), it will assume it''s a post. Raises an exception of neither are found. The second filter helps ensure that someone didn''t mess with the url. Does this help? Jeff purpleworkshops.com softiesonrails.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---