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
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
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
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.
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 ?
Fred
Frederick C. 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 ?
Fred
I 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.
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_dim
Thanks guys, hope this will help someone in future
~~Aki
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, with
IIRC that is fixed with 2.0. Yay.
–
Jay L. |
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