Ah. Well, although find_by_sql returns a Collection, the Query of that collection (@collection.query) is nil. So when you try to chain calls on that query... it's chaining to nil, and thus just defaulting to whatever the chained query is.
So when you're doing a count on @collection, you get an unrestricted count equivalent to MyModel.count. :( -T On Wed, Dec 1, 2010 at 12:18 PM, dev2 <[email protected]> wrote: > Maybe that is whats happening here. I'm not getting the result I > expect when I use .count. Is this what I /should/ expect? > > Thanks again! > > #Mymodel.rb > Class Mymodel > .... > def self.get_certain_records > find_by_sql("select .....") > end > end > > #index.html.haml > =Mymodel.get_certain_records.count # outputs 15 > -Mymodel.get_certain_records.each do |mymodel| # outputs ONLY 14 > records (the ones I expect to see) > =mymodel.id > > > > On Dec 1, 10:03 am, Mark Richman <[email protected]> wrote: > > The find_by_sql method only works if all the columns from your > > arbitrary query are mapped/mappable to your model's DM properties, no? > > The DataMapper::Collection will be an array of your Model, as opposed > > to what I did above. I'm not saying I'm more correct, but if you have > > results that don't cleanly map to your Model's DM properties, it's an > > option. > > > > On Dec 1, 9:55 am, Ted Han <[email protected]> wrote: > > > > > > > > > > > > > > > > > Check out the find_by_sql method in dm-ar-finders. > > > > > This is exactly what find_by_sql does. > > > > > On Wed, Dec 1, 2010 at 9:33 AM, dev2 <[email protected]> wrote: > > > > I have a very complex query that runs much more efficiently with > > > > direct repository access. So it returns a Struct which makes sense, > > > > but that means my views that use that data have to process as an > array > > > > instead of a DataMapper object. It would be nice if I could convert > > > > the Struct to a DataMapper object to have consistency in my views. > > > > > > Any advice/ideas/etc? > > > > > > Thanks! > > > > > > -- > > > > 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]<datamapper%[email protected]> > <datamapper%2bunsubscr...@googlegrou ps.com> > > > > . > > > > For more options, visit this group at > > > >http://groups.google.com/group/datamapper?hl=en. > > -- > 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]<datamapper%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/datamapper?hl=en. > > -- 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.
