Datamapper lazy loading

38 views
Skip to first unread message

abhishek....@gmail.com

unread,
Sep 9, 2013, 12:18:27 AM9/9/13
to datam...@googlegroups.com
I am new to Datamapper. I have written a Rails application in which I have used three models A, B and C.

   class A
      include DataMapper::Resource
      property :id, Serial
      has n, :cs, :through => Resource
    end
   
    class C
      include DataMapper::Resource
      property :id, Serial
      has n, :c_as
      has n, :as, :through => :c_as
    end

    class B
      include DataMapper::Resource
      belongs_to :a, :required => true
      belongs_to :c, :required => true
    end


Now as part of index action of my as_controller in index.html.haml I have used the following codes:

    -a.c.each do |c|
      ...

In this a.c.each lazy loading happen, so in each iteration unnecessarily the statement a.c is being evaluated that takes too much time, even if I same a.c to a third temporary object like b = a.c  and use b.each then also lazy loading happens on this line only. Is there any way to avoid that so that the statement is evaluated only once just before this block?
Reply all
Reply to author
Forward
0 new messages