How about this (simple example): http://pastie.org/975646

require 'rubygems'
require 'dm-core'
require 'dm-types'

DataMapper.setup(:default, 'sqlite3::memory:')

class Car
  include DataMapper::Resource

  property :id, Serial
  property :title, String, :required => true
  has 4, :tires

  def front_left_tire
    self.tires.first(:type => :front_left)
  end

  def front_right_tire
    self.tires.first(:type => :front_right)
  end

  def rear_left_tire
    self.tires.first(:type => :rear_left)
  end

  def rear_right_tire
    self.tires.first(:type => :rear_right)
  end

  def to_s
    @title
  end
end

class Tire
  include DataMapper::Resource
  property :car_id, Integer, :key => true
  property :type,
Enum[:front_left, :front_right, :rear_left, :rear_right], :key => true

  belongs_to :car

  def to_s
    "This is a #[email protected]_s} tire of '#...@car}' car. ID:
#{self.object_id}"
  end
end

DataMapper::auto_migrate!

c = Car.new(:title => "Super Car")
c.tires = [
  Tire.new(:type => :front_left),
  Tire.new(:type => :front_right),
  Tire.new(:type => :rear_left),
  Tire.new(:type => :rear_right)
]

if c.save
  puts "Car: #{c.inspect}"
  puts c.front_left_tire
  puts c.front_right_tire
  puts c.rear_left_tire
  puts c.rear_right_tire
end

------------------------------------------------------------------------------

Output:

Car: #<Car @id=1 @title="Super Car">
This is a front_left tire of 'Super Car' car. ID: -608927078
This is a front_right tire of 'Super Car' car. ID: -608935928
This is a rear_left tire of 'Super Car' car. ID: -608936208
This is a rear_right tire of 'Super Car' car. ID: -608936618

On May 16, 10:23 pm, Sylvain <[email protected]> wrote:
> Hi,
> when I use what you said it's working but I have a 'tire_id' in the
> SQL DB and then I can't save...
> Any way to suppress it?
>
> On Mar 23, 4:18 pm, Jonathan Stott <[email protected]> wrote:
>
>
>
> > On Tue, 23 Mar 2010 03:08:53 -0700 (PDT)
>
> > Sylvain <[email protected]> wrote:
> > > Hi all,
> > > I'm having the following model:
>
> > > Class Car
> > > has 1 :front_left_tire, 'Tire'
> > > has 1 :front_right_tire, 'Tire'
> > > has 1 :back_left_tire, 'Tire'
> > > has 1 :back_right_tire, 'Tire'
>
> > > ClassTire
> > > belongs_to :car
>
> > > when I do
>
> > >tire=Tire.new
> > > car.front_left_tire = car
> > >tire.save
>
> > > if after I look at for example car.back_right_tire, the object is
> > > equal to front_left_tire :/
>
> > > Instead of having 4 differents objects, I have 4 differents way to
> > > access the same object...
>
> > > So, the question is, how do I do it?
>
> > > thanks in advance,
>
> > I suspect the answer might be 'counter-intuitively'.  Having the car 
> > belong_to atire, which has 1 car should get the result you expect.
>
> > Regards
> > Jon
>
> --
> You received this message because you are subscribed to the Google Groups 
> "DataMapper" 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 this group 
> athttp://groups.google.com/group/datamapper?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" 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 this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to