[Nitro] OG vs Active Record
Robert Mela
rob at robmela.com
Mon Aug 20 16:07:03 EDT 2007
That's a very good approach and I'm considering it for some situations
if I can ever unravel the business logic that's stretched across several
models.
In other cases I would perform per-class selects into hashes or arrays
and loop through them at the controller level. Separating the logic
makes it a lot more transparent, since often times much of the logic
only applies in a particular area, and there's little sense in
generalizing it by embedding it in the model.
It's all OK, and it's a tough call as to where it should be done.
What I would love is an ORM that could cache records locally, perhaps
indexed in memory on certain criteria, so that it would query the local
cache based on criteria rather than generating round-trip SQL.
ActiveRecord has a :cacheable option somewhere, but it caches using the
SQL query string as a key. The cache is useless if you need to
retrieve the same objects as part of a different query.
For tables of just a few thousand records a local, in-memory cache would
be fantastic. Nitro is much better suited to that than Rails because
you only need one in-process cache can be read by many concurrent threads.
Bill Kelly wrote:
>
> def self.top_suicides_list(victim_name_str, method_str, servername_str, date=Date.today, limit=10)
> date = get_insert_date(date)
> __total__ = STATS_TOTAL_NAME
>
> @playername_total ||= Playername.find_or_create_by_playername(__total__)
> @servername_total ||= Servername.find_or_create_by_servername(__total__)
> date_str = "#{date.year}-#{date.month}-#{date.mday}"
>
> if victim_name_str.nil?
> victim = nil
> elsif victim_name_str == __total__
> victim = @playername_total
> else
> victim = PlayerSeen.find_closest_playername(victim_name_str)
> return [] unless victim
> end
>
> if servername_str.nil?
> servername = nil
> elsif servername_str == __total__
> servername = @servername_total
> else
> servername = Servername.find_by_servername(servername_str)
> return [] unless servername
> end
>
> ptotal = @playername_total
> stotal = @servername_total
>
> sql =
> "SELECT P1.playername AS victim, " +
> "FRAG.method, SV.servername AS server, FRAG.count " +
> "FROM #{self.table} FRAG, #{Playername.table} P1, " +
> "#{Servername.table} SV " +
> "WHERE " +
> (victim ? "FRAG.victim_oid = #{victim.oid}" : "FRAG.victim_oid <> #{ptotal.oid}") +
> " AND " + (servername ? "FRAG.servername_oid = #{servername.oid}" : "FRAG.servername_oid <> #{stotal.oid}") +
> " AND " + (method_str ? "FRAG.method = '#{method_str}'" : "FRAG.method <> '#{__total__}'") +
> " AND FRAG.date = '#{date_str}' " +
> "AND P1.oid = FRAG.victim_oid " +
> "AND SV.oid = FRAG.servername_oid " +
> "ORDER BY FRAG.count DESC"
> sql << " LIMIT #{limit}" if limit
> res = ogstore.query(sql)
> rows = []
> res.each_row {|row,dummy| rows << row}
> rows
> end
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: rob.vcf
Type: text/x-vcard
Size: 116 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/nitro-general/attachments/20070820/df47bd18/attachment.vcf
More information about the Nitro-general
mailing list