improvement to has_many

4 views
Skip to first unread message

Lenny

unread,
Nov 23, 2009, 12:42:31 PM11/23/09
to SimpleRecord
Building my first SimpleRecord-based app, I ran into a bit of
frustration because I couldn't use methods like map and length on
has_many, but I found a simple solution that I think the rest of you
might like and may want to integrate into the project's next release:

# test will fail:
class Bar < SimpleRecord::Base end
class Foo < SimpleRecord::Base
has_many :bars
end
f = Foo.create
f.bars << Bar.create
f.bars.length # error but should return 1
f.bars.map{|b|b} # error but should return [Bar]

module SimpleRecord
class Base
def method_missing(name, *args, &block)
load.respond_to?(name) ? load.__send__(name,*args,&block) : super
(name,*args,&block)
end
end
end
# now the tests will pass

Chad Arimura

unread,
Nov 23, 2009, 3:18:45 PM11/23/09
to simple...@googlegroups.com
Lenny -

Thanks for the tip! f.bars.count should work as is, and f.bars.size
is an alias of count, but there is no length method. Are count() and
size() sufficient?

Cheers,
Chad
> --
>
> You received this message because you are subscribed to the Google Groups "SimpleRecord" group.
> To post to this group, send email to simple...@googlegroups.com.
> To unsubscribe from this group, send email to simple-recor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/simple-record?hl=.
>
>
>

Travis Reeder

unread,
Nov 23, 2009, 3:59:29 PM11/23/09
to simple...@googlegroups.com
Hi Lenny,

There is a length method actually, so not sure why that doesn't work for you. If you could provide a stacktrace for the error, that would be great.  The results array currently doesn't support all the standard ruby array methods because it's actually an instance if SimpleRecord::ResultsArray which is primarily there to support lazy loading functionality (loads one set of results a time and only gets more if they are actually needed/used). http://github.com/appoxy/simple_record/blob/master/lib/results_array.rb 

Map is certainly not there yet, but I suppose it could be added without too much effort.

Travis

Chad Arimura

unread,
Nov 23, 2009, 4:15:28 PM11/23/09
to simple...@googlegroups.com

Hey Travis,

I think has many returns activesdb_something object and not results array.  Maybe we should switch that over for continuity as long as its seamless.

On Nov 23, 2009 12:59 PM, "Travis Reeder" <tre...@gmail.com> wrote:

Hi Lenny,

There is a length method actually, so not sure why that doesn't work for you. If you could provide a stacktrace for the error, that would be great.  The results array currently doesn't support all the standard ruby array methods because it's actually an instance if SimpleRecord::ResultsArray which is primarily there to support lazy loading functionality (loads one set of results a time and only gets more if they are actually needed/used). http://github.com/appoxy/simple_record/blob/master/lib/results_array.rb 


Map is certainly not there yet, but I suppose it could be added without too much effort.

Travis

On Mon, Nov 23, 2009 at 12:18 PM, Chad Arimura <cari...@gmail.com> wrote: > > Lenny - > > Thanks...

-- You received this message because you are subscribed to the Google Groups "SimpleRecord" group. ...

Travis Reeder

unread,
Nov 23, 2009, 4:39:28 PM11/23/09
to simple...@googlegroups.com
Ahh yes, just checked it out and you are right. Should definitely be switched to ResultsArray.  I created a ticket here: http://appoxy.lighthouseapp.com/projects/38366-simplerecord/tickets/13-make-has_many-use-resultsarray-rather-than-old-sub_array

Lenny, do you have a good unit test for has_many we can use while fixing this?

Travis

Lenny Turetsky

unread,
Nov 24, 2009, 1:30:48 AM11/24/09
to simple...@googlegroups.com
Thanks for your speedy replies, Travis and Chad.

I was over-simplifying a bit. The method I really needed wasn't
length() or even map(); it was each_with_index().
Really, implementing method_missing() is the way to go. Implementing
each method when somebody complains about its absence is too much of a
patchwork.

> do you have a good unit test for has_many we can use while fixing this?

Not sure what you have in mind, but I'd like to test everything
returned by ActiveRecord.has_many.public_methods. After all, we're
trying to be a replacement for ActiveRecord.

Thanks for listening,

Lenny
Reply all
Reply to author
Forward
0 new messages