I have a polymorphic class Event class Event < ActiveRecord::Base belongs_to :taskable, :polymorphic => true so an event.instance has : event.taskable_type and event.taskable_id i.e. event[:taskable_type] = "Project" event[:taskable_id] = 2 Is it possible to localize the taskable_type ( "Project") using Model.model_name.human ? taskable_type is a string... I tried to get it from the instance , event.taskable.model_name.human, but model_name is not an AR instance method ( NoMethodError: undefined method `human_name'' for #<Project...> -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
See the method constantize. On Thu, Feb 9, 2012 at 8:38 PM, Erwin <yves_dufour-ee4meeAH724@public.gmane.org> wrote:> I have a polymorphic class Event > > class Event < ActiveRecord::Base > belongs_to :taskable, :polymorphic => true > > so an event.instance has : event.taskable_type and > event.taskable_id > i.e. event[:taskable_type] = "Project" event[:taskable_id] = 2 > > Is it possible to localize the taskable_type ( "Project") using > Model.model_name.human ? taskable_type is a string... > > I tried to get it from the instance , > event.taskable.model_name.human, but model_name is not an AR instance > method > ( NoMethodError: undefined method `human_name'' for #<Project...> > > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, Feb 9, 2012 at 11:38 PM, Erwin <yves_dufour-ee4meeAH724@public.gmane.org> wrote:> I have a polymorphic class Event > > class Event < ActiveRecord::Base > belongs_to :taskable, :polymorphic => true > > so an event.instance has : event.taskable_type and > event.taskable_id > i.e. event[:taskable_type] = "Project" event[:taskable_id] = 2 > > Is it possible to localize the taskable_type ( "Project") using > Model.model_name.human ? taskable_type is a string... > > I tried to get it from the instance , > event.taskable.model_name.human, but model_name is not an AR instance > method > ( NoMethodError: undefined method `human_name'' for #<Project...> >I think I found it ... (taskable is the instance, but model_name needs to run on the class, so add .class): Contact belongs_to :person 011:0> contact.person.model_name NoMethodError: undefined method `model_name'' for #<Person:0xaa16a44> ... 012:0> contact.person.class.model_name => "Person" I have not tried on polymorphic, but I presume it would work too. HTH, Peter -- *** Available for a new project *** Peter Vandenabeele http://twitter.com/peter_v http://rails.vandenabeele.com http://coderwall.com/peter_v -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
thanks Everaldo that''s it ... ( maybe the first time I use it... !) On Feb 9, 11:42 pm, Everaldo Gomes <everaldo.go...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> See the method constantize. > > > > > > > > On Thu, Feb 9, 2012 at 8:38 PM, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote: > > I have a polymorphic class Event > > > class Event < ActiveRecord::Base > > belongs_to :taskable, :polymorphic => true > > > so an event.instance has : event.taskable_type and > > event.taskable_id > > i.e. event[:taskable_type] = "Project" event[:taskable_id] = 2 > > > Is it possible to localize the taskable_type ( "Project") using > > Model.model_name.human ? taskable_type is a string... > > > I tried to get it from the instance , > > event.taskable.model_name.human, but model_name is not an AR instance > > method > > ( NoMethodError: undefined method `human_name'' for #<Project...> > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
as per your answer + Evaraldo''s one : event.taskable.class.constantize.model_name on polymorphic works On Feb 10, 12:05 am, Peter Vandenabeele <pe...-jNuWw7i2w7syMbTcgqFhxg@public.gmane.org> wrote:> On Thu, Feb 9, 2012 at 11:38 PM, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote: > > I have a polymorphic class Event > > > class Event < ActiveRecord::Base > > belongs_to :taskable, :polymorphic => true > > > so an event.instance has : event.taskable_type and > > event.taskable_id > > i.e. event[:taskable_type] = "Project" event[:taskable_id] = 2 > > > Is it possible to localize the taskable_type ( "Project") using > > Model.model_name.human ? taskable_type is a string... > > > I tried to get it from the instance , > > event.taskable.model_name.human, but model_name is not an AR instance > > method > > ( NoMethodError: undefined method `human_name'' for #<Project...> > > I think I found it ... (taskable is the instance, but model_name needs to > run on the class, so add .class): > > Contact belongs_to :person > > 011:0> contact.person.model_name > NoMethodError: undefined method `model_name'' for #<Person:0xaa16a44> > ... > 012:0> contact.person.class.model_name > => "Person" > > I have not tried on polymorphic, but I presume it would work too. > > HTH, > > Peter > > -- > *** Available for a new project *** > > Peter Vandenabeelehttp://twitter.com/peter_vhttp://rails.vandenabeele.comhttp://coderwall.com/peter_v-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.