1

I am using datamapper in association with postgres

I have 2 classes defined like this:

class Dep
  include DataMapper::Resource

  property :id, Serial
  has 1, :rec, 'Rec'
end 

class Rec
  include DataMapper::Resource  

  property :id, Serial
  belongs_to :dep, 'Dep'

end

When I try to do so and access the dep model I get an error: undefined method 'rec_id' for #

As far as my research goes the model that is created does not have a one to one relationship. Instead it has a one to many relationship. I think that is the problem. Is there any way to create a one to one relationship in datamapper?

Further, it does not let me use :required => false with 'has 1'

Using belongs_to both side does not create a proper relationship. It creates to relationships instead which is undesirable. Can anyone help me with this issue?

Chris Travers
  • 25,424
  • 6
  • 65
  • 182
inertia
  • 404
  • 4
  • 15
  • Did you definitely call `DataMapper.finalize` ? – d11wtq Sep 01 '12 at 15:16
  • Yes i did. And it works fine with has n. Only has 1 seems to have a problem – inertia Sep 03 '12 at 04:54
  • 1
    Please edit the question to include an example showing how you're using this. `rec_id` should not exist anywhere. `Rec` should have a `dep_id` (as this is how `has 1` works). – d11wtq Sep 05 '12 at 15:04
  • Note that you can use a `belongs_to` on both sides of the join. – d11wtq Sep 05 '12 at 15:04
  • Using belongs_to on both the sides does not create an inverse relationship which means that two separate relationships will be made which is not desirable as I already mentioned. I am not using rec_id anywhere. I have an application which has ember on the front end which queries for required records to the datamapper. So, when in ember I query any dep record I get the above error. I have tried it using rake console as well to ensure its not an ember side problem and I get the same error. – inertia Sep 06 '12 at 03:40

0 Answers0