Here it functions on irb.
I create the model's tables and run that on irb
ruby-1.9.3-p0 :034 > Dep.new
=> #<Dep @id=nil>
ruby-1.9.3-p0 :035 > Rec.new
=> #<Rec @id=nil @dep_id=nil>
ruby-1.9.3-p0 :036 > r = Rec.new
=> #<Rec @id=nil @dep_id=nil>
ruby-1.9.3-p0 :037 > r.de
r.define_singleton_method r.dep= r.dep_id=
r.destroy!
r.dep r.dep_id r.destroy
r.destroyed?
ruby-1.9.3-p0 :037 > r.dep = Dep.new
=> #<Dep @id=nil>
ruby-1.9.3-p0 :038 > r.save
=> true
ruby-1.9.3-p0 :039 > r
=> #<Rec @id=1 @dep_id=1>
ruby-1.9.3-p0 :040 > Rec.first
=> #<Rec @id=1 @dep_id=1>
ruby-1.9.3-p0 :041 > Rec.first.dep
=> #<Dep @id=1>
ruby-1.9.3-p0 :042 > Dep.first
=> #<Dep @id=1>
ruby-1.9.3-p0 :043 > Dep.first.re
Display all 110 possibilities? (y or n)
ruby-1.9.3-p0 :043 > Dep.first.rec
=> #<Rec @id=1 @dep_id=1>
and in the other direction
ruby-1.9.3-p0 :059 > s = Dep.new
=> #<Dep @id=nil>
ruby-1.9.3-p0 :060 > s.rec = Rec.new
=> #<Rec @id=nil @dep_id=nil>
ruby-1.9.3-p0 :061 > s.save
=> true
ruby-1.9.3-p0 :062 > Dep.all
=> [#<Dep @id=1>, #<Dep @id=2>, #<Dep @id=3>]
ruby-1.9.3-p0 :063 > Rec.all
=> [#<Rec @id=1 @dep_id=2>, #<Rec @id=2 @dep_id=1>, #<Rec @id=3
@dep_id=3>]
ruby-1.9.3-p0 :064 >
ps.: I'd created another dep and rec too.
2012/9/3 inertia <[email protected]>
> 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?
>
> --
> You received this message because you are subscribed to the Google Groups
> "DataMapper" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/datamapper/-/6CBSyCoH_e4J.
> 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.
>
--
------------------------------------------
1. Notebook para você ficar livre e programar a vontade: R$ 2300
2. Curso de Programação/SysAdmin/DBA: R$ 5000
3. Pedir a solução pronta para um problema numa lista de discussão: Não
tem preço !
E para todas as outras existe RTFM, STFW e LMGTFY
--
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.