It appears that I have the models and migrations set up correctly. In case anyone is wondering why i set it up like this I had it working with just a contact model and this will save a bunch of coding changes to my already working CRUD ajax inline contact form. I am adding a contrator model so I would like to have a polymorphic setup so I can differentiate the two types of people in the form. I have a Person that is polymorphic,, it will have a contact and contractor type. class Person < ActiveRecord::Base belongs_to :contactable, :polymorphic => true end class Contact < ActiveRecord::Base has_many :people, :as => :contactable end When I do: person = Person.create(params[:person]) @contact = Contact.create(params[:contact]) person.contactable = @contact It goes without an error but puts NIL in the contactable_type and contactable_id columns. Anyone have an idea why? I spent a couple hours with this earlier. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
In case anyone is wondering here is the migration: class CreatePeople < ActiveRecord::Migration def self.up create_table :people do |t| t.integer :company_id t.references :contactable, :polymorphic => true t.timestamps end end def self.down drop_table :people end end On Apr 18, 7:50 pm, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> It appears that I have the models and migrations set up correctly. > > In case anyone is wondering why i set it up like this I had it working > with just a contact model and this will save a bunch of coding changes > to my already working CRUD ajax inline contact form. I am adding a > contrator model so I would like to have a polymorphic setup so I can > differentiate the two types of people in the form. > > I have a Person that is polymorphic,, it will have a contact and > contractor type. > > class Person < ActiveRecord::Base > belongs_to :contactable, :polymorphic => true > end > > class Contact < ActiveRecord::Base > has_many :people, :as => :contactable > end > > When I do: > > person = Person.create(params[:person]) > @contact = Contact.create(params[:contact]) > person.contactable = @contact > > It goes without an error but puts NIL in the contactable_type and > contactable_id columns. > > Anyone have an idea why? I spent a couple hours with this earlier.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Apr 19, 1:50 am, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> person = Person.create(params[:person]) > @contact = Contact.create(params[:contact]) > person.contactable = @contact > > It goes without an error but puts NIL in the contactable_type and > contactable_id columns. > > Anyone have an idea why? I spent a couple hours with this earlier.Are you saving the person record after having assigned the contactable ? Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The Create method saves on creation does it not? On Sun, Apr 19, 2009 at 12:27 PM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On Apr 19, 1:50 am, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > person = Person.create(params[:person]) > > @contact = Contact.create(params[:contact]) > > person.contactable = @contact > > > > It goes without an error but puts NIL in the contactable_type and > > contactable_id columns. > > > > Anyone have an idea why? I spent a couple hours with this earlier. > > Are you saving the person record after having assigned the > contactable ? > > Fred > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Here is my stand alone script i just tried, still putting NIL in the contactable columns: I even tried person.contactable.save to no avail. RAILS_ENV = ''development'' require File.dirname(__FILE__) + ''/../config/boot'' require File.dirname(__FILE__) + ''/../config/environment'' @contact = Contact.create(:name => ''moo'') @contact.save person = Person.create(:company_id => ''1'') person.save person.contactable = @contact On Sun, Apr 19, 2009 at 12:27 PM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On Apr 19, 1:50 am, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > person = Person.create(params[:person]) > > @contact = Contact.create(params[:contact]) > > person.contactable = @contact > > > > It goes without an error but puts NIL in the contactable_type and > > contactable_id columns. > > > > Anyone have an idea why? I spent a couple hours with this earlier. > > Are you saving the person record after having assigned the > contactable ? > > Fred > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ok, I was looking at the DB for the contactable.type and id. Why does it not show up there but when you query it is is there? On Sun, Apr 19, 2009 at 12:27 PM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On Apr 19, 1:50 am, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > person = Person.create(params[:person]) > > @contact = Contact.create(params[:contact]) > > person.contactable = @contact > > > > It goes without an error but puts NIL in the contactable_type and > > contactable_id columns. > > > > Anyone have an idea why? I spent a couple hours with this earlier. > > Are you saving the person record after having assigned the > contactable ? > > Fred > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I did the example in rails recipe. I can retrieve the addressable_id and type when I create itfrom the console, but when I go log out and go back and try to retrieve them they give back NIL just like the DB shows. Is the ID and TYPE supposed to show up in the DB? I am still getting NIL for the polymorphic columns. Stumped... On Apr 19, 7:18 pm, Chris Habgood <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ok, I was looking at the DB for the contactable.type and id. Why does it > not show up there but when you query it is is there? > > On Sun, Apr 19, 2009 at 12:27 PM, Frederick Cheung < > > frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Apr 19, 1:50 am, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > person = Person.create(params[:person]) > > > @contact = Contact.create(params[:contact]) > > > person.contactable = @contact > > > > It goes without an error but puts NIL in the contactable_type and > > > contactable_id columns. > > > > Anyone have an idea why? I spent a couple hours with this earlier. > > > Are you saving the person record after having assigned the > > contactable ? > > > Fred--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ok, I did a new method instead of create and then the last thing I did was a save and it works. Anybody know why? On Apr 19, 9:14 pm, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I did the example in rails recipe. I can retrieve the addressable_id > and type when I create itfrom the console, but when I go log out and > go back and try to retrieve them they give back NIL just like the DB > shows. Is the ID and TYPE supposed to show up in the DB? I am still > getting NIL for the polymorphic columns. Stumped... > > On Apr 19, 7:18 pm, Chris Habgood <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Ok, I was looking at the DB for the contactable.type and id. Why does it > > not show up there but when you query it is is there? > > > On Sun, Apr 19, 2009 at 12:27 PM, Frederick Cheung < > > > frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On Apr 19, 1:50 am, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > person = Person.create(params[:person]) > > > > @contact = Contact.create(params[:contact]) > > > > person.contactable = @contact > > > > > It goes without an error but puts NIL in the contactable_type and > > > > contactable_id columns. > > > > > Anyone have an idea why? I spent a couple hours with this earlier. > > > > Are you saving the person record after having assigned the > > > contactable ? > > > > Fred--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
dmitry.sokurenko-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2009-Apr-21 13:21 UTC
Re: polymorphic problema
Because assignment of the new parent record to a child is not immideately persisted. You need to call save to persist it in the database. So this changes only the object in memory: person.contactable = @contact This updates the database: person.contactable = @contact person.save Dmitry
I did that even with the "create" methid, no luck. I had to use the "new" method and save it for it to write to the db. On Tue, Apr 21, 2009 at 8:21 AM, dmitry.sokurenko-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org < Dmitry.Sokurenko-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Because assignment of the new parent record to a child is not > immideately persisted. You need to call save to persist it in the > database. > > So this changes only the object in memory: > person.contactable = @contact > > This updates the database: > person.contactable = @contact > person.save > > Dmitry > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---