Michael Pavling wrote:
> Coming back to my first thought when I read your code: WTF? What are
> you *trying* to do?! Why are you hoping to achieve with that
> find_by_sql? Are you trying to find the first record you created? If
> so, you need to learn some SQL:
> 
> b = Blurg.find_by_sql("SELECT * from blurgs where name = 'tic'")
> 
> If that's not what you're trying to do, please explain.

Michael:

The point of my question is that the save! silently fails, which 
surprises me.
save! returns true yet doesn't save the record: how is this not a bug?

But since you asked (and since you've impugned my knowledge of SQL :), 
what I'm really trying to do is marshal a bunch of fields from other 
tables through a join.  The real query is more like:

class AnalysisDatum < ActiveRecord::Base
... snip ...
  def self.daily_metered_service(analysis_set_id, *metered_service_ids)
    return nil unless metered_service_ids
    service_ids = Utils.commify(metered_service_ids)
    query1 = <<QUERY1
... snip ...
QUERY1
    query2 = <<QUERY2
    SELECT td.id as start_time_id,
           DATE(td.datetime) AS date,
           bills.premise_id AS premise_id,
           #{analysis_set_id} AS analysis_set_id,
           bills.resource AS resource,
           SUM(bills.monthly_quantity/bills.billing_days) AS quantity,
           SUM(bills.monthly_cost/bills.billing_days) AS cost
      FROM time_dimensions AS td
INNER JOIN (#{query1}) AS bills
     WHERE td.datetime BETWEEN bills.start_incl AND bills.end_incl
  GROUP BY date, premise_id, resource
QUERY2
    AnalysisDatum.find_by_sql(query2)
  end
end

Each of the fields named in the query2 SELECT are names of field in a 
"natural" AnalysisDatum record.

I understand that the records returned by this query are not stored in 
the database, but I expected that this would work:

  records = AnalysisDatum.daily_metered_service(as_id, ms_ids)
  records.map {|r| r.save!}

... but as I said, this neither saves the records nor signals any error. 
If that's not a bug, then it's at least unexpected.  (r.readonly? 
returns nil as well...)

I'm certain that I can create a new, empty AnalysisDatum records and 
copy the fields from the records returned by the query, but I still want 
to be convinced this is not a bug.

Thanks.
-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to