Hello there, I''m developing an application which has models like this: class Vehicle < ActiveRecord::Base belongs_to :internal_dim, :class_name => "Dimension" def before_create() idim = Dimension.create self.internal_dim_id = idim.id #self.create_internal_dim # Doesn''t work end end class Dimension < ActiveRecord::Base has_one :vehicle end That is, creating the vehicle should create a dimension entry (yeah, i know it''s strange, but actually dimension table is shared among different things, so the relationship is inverted). Anyway, it happens that when a vehicle is created, the internal dimension id is correctly set:>>> v = Vehicle.create >>> v.internal_dim_id=> 3 But the method doesn''t get the corresponding value:>>> v.internal_dim=> nil While the records exists:>> Dimension.find(v.internal_dim_id)=> #<Dimension:0xb7042678 @attributes={blah}> Have you any idea? Thanks ~~Ale -- 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 -~----------~----~----~----~------~----~------~--~---
Roderick van Domburg
2007-Oct-08 10:53 UTC
Re: belongs_to association doesn''t work with _id set
Alessandro Re wrote:> Hello there, > I''m developing an application which has models like this: > > class Vehicle < ActiveRecord::Base > belongs_to :internal_dim, :class_name => "Dimension" > > def before_create() > idim = Dimension.create > self.internal_dim_id = idim.id > #self.create_internal_dim # Doesn''t work > end > end > > class Dimension < ActiveRecord::Base > has_one :vehicle > end > > That is, creating the vehicle should create a dimension entry (yeah, i > know it''s strange, but actually dimension table is shared among > different things, so the relationship is inverted). > > Anyway, it happens that when a vehicle is created, the internal > dimension id is correctly set: > >>>> v = Vehicle.create >>>> v.internal_dim_id > => 3 > > But the method doesn''t get the corresponding value: > >>>> v.internal_dim > => nil > > While the records exists: > >>> Dimension.find(v.internal_dim_id) > => #<Dimension:0xb7042678 @attributes={blah}> > > Have you any idea?It''s because of the has_one in Dimension. It causes the reference to be held by the other object. Either change it into has_many or move the foreign key column over to the Dimension table. -- Roderick van Domburg http://www.nedforce.com -- 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 -~----------~----~----~----~------~----~------~--~---
Roderick van Domburg wrote:> It''s because of the has_one in Dimension. It causes the reference to be > held by the other object. Either change it into has_many or move the > foreign key column over to the Dimension table.Argh! You''re right! Thanks very much :) -- 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 -~----------~----~----~----~------~----~------~--~---
Alessandro Re wrote:> Roderick van Domburg wrote: >> It''s because of the has_one in Dimension. It causes the reference to be >> held by the other object. Either change it into has_many or move the >> foreign key column over to the Dimension table. > > Argh! You''re right! > Thanks very much :)No, sorry. It doesn''t work with has_many :( And I can''t move the foreign key into Dimension table, so I must find something else. -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Oct-08 13:14 UTC
Re: belongs_to association doesn''t work with _id set
On 8 Oct 2007, at 11:01, Alessandro Re wrote:> > Hello there, > I''m developing an application which has models like this: > > class Vehicle < ActiveRecord::Base > belongs_to :internal_dim, :class_name => "Dimension" > > def before_create() > idim = Dimension.create > self.internal_dim_id = idim.id > #self.create_internal_dim # Doesn''t work > end > endWhy not self.internal_dim = idim ? 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 8 Oct 2007, at 11:01, Alessandro Re wrote: > >> #self.create_internal_dim # Doesn''t work >> end >> end > > Why not self.internal_dim = idim ? > > FredI found out that it doesn''t work: after the assignment the internal_dim_id is nil. Look: the actual code: def before_create() idim = Dimension.create edim = Dimension.create self.internal_dim_id = idim.id self.external_dim_id = edim.id puts(" Dimensions: int #{internal_dim_id} ext #{external_dim_id}") end output: #<Company:0xb711d494> created #<Dimension:0xb70d02e8> created #<Dimension:0xb70c8480> created Dimensions: int 1 ext 2 Changing with: def before_create() idim = Dimension.create #edim = Dimension.create self.internal_dim_id = idim.id #self.external_dim_id = edim.id #self.create_internal_dim self.create_external_dim puts(" Dimensions: int #{internal_dim_id} ext #{external_dim_id}") end output: #<Company:0xb713510c> created #<Dimension:0xb70ebd04> created #<Dimension:0xb70e5634> created Dimensions: int 1 ext As you see, external dim id is not set, even if the dimension is created. I didn''t figure out why this happen, yet. But this is a minimal issue for me now, I need to understand before why internal_dim doesn''t return the associated dimension, even if the id is set. -- 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 -~----------~----~----~----~------~----~------~--~---
Ok, solved. The problem was that> class Vehicle < ActiveRecord::Base > belongs_to :internal_dim, :class_name => "Dimension"doesn''t set automatically the foreign key to internal_dim_id when class_name is used. So, with> belongs_to :internal_dim, :foreign_key => "internal_dim_id", :class_name => "Dimension"it works, and probably (didn''t try yet), it will fix the assignment problems:>> #self.create_internal_dim # Doesn''t work > Why not self.internal_dim = idim ?and also>> self.create_intternal_dimThanks guys, hope this will help someone in future ~~Aki -- 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 Mon, 8 Oct 2007 16:21:06 +0200, Alessandro Re wrote:> Ok, solved. The problem was that > >> class Vehicle < ActiveRecord::Base >> belongs_to :internal_dim, :class_name => "Dimension" > > doesn''t set automatically the foreign key to internal_dim_id when > class_name is used. So, withIIRC that is fixed with 2.0. Yay. -- Jay Levitt | Boston, MA | My character doesn''t like it when they Faster: jay at jay dot fm | cry or shout or hit. http://www.jay.fm | - Kristoffer --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---