http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-becomes

class User < Person
end

u = User.first
u.class.name #=> User

p = u.becomes(Person)
p.class.name #=> Person



Dheeraj Kumar


On Thursday 9 August 2012 at 12:38 AM, Mohamad El-Husseini wrote:

> Sorry, can you clarify your post a little? What does AR::Base#becomes exactly 
> mean? I'm relatively new to some of this stuff!
> 
> On Wednesday, August 8, 2012 3:03:38 PM UTC-4, Dheeraj Kumar wrote:
> > It's simple. Use AR::Base#becomes
> > 
> > 
> > Dheeraj Kumar
> > 
> > 
> > On Thursday 9 August 2012 at 12:20 AM, Mohamad El-Husseini wrote:
> > 
> > > I'm using Rails 3.2. And I'm pretty sure you can. Basically, to get a 
> > > form to post to the right model I need to set it up generically. My 
> > > initial form was:
> > > 
> > > = simple_form_for @user do |f|
> > > 
> > > To make it work I had to change it to this:
> > > 
> > > 
> > > = simple_form_for :user, url: user_path(@user) do |f|
> > > To work with link helpers, I added this as resource:
> > > 
> > > resources :owners, path: 'users', controller: 'users'
> > > 
> > > 
> > > 
> > > 
> > > 
> > > On Wednesday, August 8, 2012 10:43:47 AM UTC-4, Peter wrote:
> > > > On Tuesday, August 7, 2012 11:13:56 AM UTC-5, Mohamad El-Husseini wrote:
> > > > > I want my helpers to generate paths using a superclass instead of the 
> > > > > subclasses. Assuming I have Owner and Member that both inherit from 
> > > > > User, rails will use the current objects class name when generating 
> > > > > paths:
> > > > > 
> > > > > Let's say current_user is a mod: <%= link_to current_user.name 
> > > > > (http://current_user.name), current_user %> will generate "/mod/:id". 
> > > > > I want to force it to generate "/user/:id" regardless of the subclass.
> > > > > 
> > > > > I can name the path:
> > > > > <%= link_to current_user.name (http://current_user.name), 
> > > > > user_path(current_user) %>
> > > > > 
> > > > > But I still want to use the convenience of just passing the object:
> > > > > <%= link_to current_user.name (http://current_user.name), 
> > > > > current_user %>
> > > > > 
> > > > > 
> > > > > Is this possible?
> > > > > 
> > > > 
> > > > You didn't specify the rails version, but I'm pretty sure the answer is 
> > > > no.  In Rails 2.3.14 your link to is eventually calling url_for, which 
> > > > calls polymorphic_url (through polymorphic_path) since you're not 
> > > > passing it a String, Hash or the symbol :back 
> > > > (https://github.com/rails/rails/blob/v2.3.14/actionpack/lib/action_view/helpers/url_helper.rb#L76).
> > > >   In turn that eventually calls build_named_route_call, which uses 
> > > > RecordIdentifier.plural_class_name(current_user), which is returning 
> > > > the class name 
> > > > (https://github.com/rails/rails/blob/v2.3.14/actionpack/lib/action_controller/polymorphic_routes.rb#L154).
> > > >   And finally that in turn eventually calls current_user.model_name 
> > > > (https://github.com/rails/rails/blob/v2.3.14/actionpack/lib/action_controller/record_identifier.rb#L100)
> > > > 
> > > > You could look into overriding what model name returns for those 
> > > > classes, but that seems really far reaching and dangerous.  I'd just 
> > > > use the path helper since that best represents what you want to do, 
> > > > which is send an Owner or Member object not to /owners/:id or 
> > > > /members/:id but to /users/:id instead; ergo the verbosity doesn't seem 
> > > > bad to me, it helps clarify.
> > > > 
> > > > As an alternative, you could also define the /owners/:id and 
> > > > /members/:id routes and point them at UsersController; not sure if 
> > > > that's okay to have the extra routes, but that allows you to keep your 
> > > > shorthand notation, has that map to the expected URL, but lets you DRY 
> > > > the underlying controller class.
> > > > 
> > > > \Peter
> > > > 
> > > > 
> > > -- 
> > > 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 [email protected] 
> > > (javascript:).
> > > To unsubscribe from this group, send email to 
> > > [email protected] (javascript:).
> > > To view this discussion on the web visit 
> > > https://groups.google.com/d/msg/rubyonrails-talk/-/-NUfQAxSGDMJ.
> > > For more options, visit https://groups.google.com/groups/opt_out.
> > >  
> > >  
> > 
> -- 
> 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 [email protected] 
> (mailto:[email protected]).
> To unsubscribe from this group, send email to 
> [email protected] 
> (mailto:[email protected]).
> To view this discussion on the web visit 
> https://groups.google.com/d/msg/rubyonrails-talk/-/zS500RJNLR4J.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to