Here it is straight from my irb (taking out some irrelevant
properties):

me=Character.get(1)
 ~ (0.000123) SELECT "id", ... FROM "characters" WHERE "id" = 1 ORDER
BY "id" LIMIT 1
=> #<Character @id=1 ... @location_id=1>
>> me.location.npcs
 ~ (0.000074) SELECT "id", ... FROM "locations" WHERE "id" = 1 ORDER
BY "id" LIMIT 1
 ~ (0.000095) SELECT "id", ... "location_id" FROM "npcs" WHERE
"location_id" = 1 ORDER BY "id"
=> [#<Npc @id=1 @name="npc1" ... @location_id=1>]
>> me.location.npcs.class
=> DataMapper::Associations::OneToMany::Collection
>> loc=Location.get(1)
 ~ (0.000733) SELECT "id" ... FROM "locations" WHERE "id" = 1 ORDER BY
"id" LIMIT 1
=> #<Location @id=1 ...>
>> npc=Npc.new(:name=>"new npc")
=> #<Npc @id=nil ... @location_id=nil>
>> loc.npcs<<npc
 ~ (0.000104) SELECT "id", ... "location_id" FROM "npcs" WHERE
"location_id" = 1 ORDER BY "id"
=> [#<Npc @id=1 @name="npc1" ... @location_id=1>, #<Npc @id=nil
@name="new npc" ... @location_id=1>]
>> npc.save
 ~ (0.038374) INSERT INTO "npcs" ("name", ... "location_id") VALUES
('new npc', ... 1)
=> true
>> me.location.npcs
=> [#<Npc @id=1 @name="npc1" ... @location_id=1>]
>> me.location.npcs.size
=> 1
>> me.location.npcs.count
 ~ (0.000074) SELECT COUNT(*) FROM "npcs" WHERE "location_id" = 1
=> 2
>> npc.location.characters
 ~ (0.000136) SELECT "id", ... "location_id" FROM "characters" WHERE
"location_id" = 1 ORDER BY "id"
=> [#<Character @id=1 ... @location_id=1 @player_id=1>]
>> npc.location.npcs.count
 ~ (0.000078) SELECT COUNT(*) FROM "npcs" WHERE "location_id" = 1
=> 2
>> npc.location.npcs.size
=> 2
>>

The real question I have here is why didn't DataMapper go back out to
the repository to when I said "me.location.npcs" below.  It did when I
asked for the count and it returned that correctly (obviously).

>> me.location.npcs
=> [#<Npc @id=1 @name="npc1" ... @location_id=1>]
>> me.location.npcs.size
=> 1
>> me.location.npcs.count
 ~ (0.000074) SELECT COUNT(*) FROM "npcs" WHERE "location_id" = 1
=> 2

On Dec 29, 2:57 pm, RailinAndWailin <waugustyn...@gmail.com> wrote:
> In "char.location.npcs" the char object is trying to access the
> location's npc object collection (hence the plural form. the location
> class has n npc objects).
> irb does show that no SELECT query is geting run everytime I try to
> query that parent collection... when i exit out of irb and come back
> in i see the correct char.location.npcs.count (and members).
> The odd thing is that when I see this happen: char.location.npcs.size !
> = char.location.npcs.count and, of course, npc.location.npcs !=
> char.location.npcs (which they should, right?)
>
> On Dec 29, 12:57 pm, RailinAndWailin <waugustyn...@gmail.com> wrote:
>
> > I hope this isn't something I'm doing out of ignorance:
>
> > class Location
> > ...
> > has n, :characters
> > has n, :npcs
>
> > class Character
> > ..
> > belongs_to :location
>
> > class Npc
> > ...
> > belongs_to :location
>
> > >>char=Character.new
> > >>loc=Location.new
> > >>loc.characters<<char
> > >>char.save
> > >>char.location
>
> > =><#Location @id=1...>
>
> > >>char.location.npcs
> > =>[ ]
> > >>npc=Npc.new
> > >>loc.npcs<<npc
> > >>npc.save
>
> > Now, why does this happen? :
>
> > >>char.location.npc
>
> > =>[ ]
>
> > Still empty, even though:
>
> > >>npc.location
>
> > =><#Location @id=1...>
>
> > >>npc.location.characters
> > >>[<#Character @id ... >]
>
> > The thing that I notice is that whe i query the collection as
> > "char.location.npcs" I don't see a SQL query going out (as though its
> > looking at a chache), though when I "char.locatin.npcs.count" I get
> > the proper return as 1 (and a query going out).
>
> > help a noob understand?
>
>

--

You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to datamap...@googlegroups.com.
To unsubscribe from this group, send email to 
datamapper+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.


Reply via email to