On 9 May 2010 16:06, Fearless Fool <li...@ruby-forum.com> wrote: > Michael Pavling wrote: > 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?
It doesn't fail... the SQL works perfectly. Go into your favourite SQL query editor and execute: UPDATE table SET field = 'value' WHERE id = '' ... what does it say? "zero rows affected"? That's a "success" according to the DB (okay - it may not be what you *want* but it's what *is* going to happen). > But since you asked (and since you've impugned my knowledge of SQL :), Well... I hate to be mean, but I can only judge by the evidence I get given ;-) > 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: > > <snip big fat queries> > > 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!} Why do you *expect* that? The 'records' array doesn't really contain AnalysisDatum records, it contains AR:Base-derived class objects with a bunch of fields of your choosing (AR is just being nice to you and mapping those fields to attributes. You could just as easily do "User.find_by_sql(BIG_FAT_QUERY)" and run exactly the same query.... you wouldn't *expect* to be able to save your calculations as User records would you? > ... 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. It's exactly what I'd expect - you've not requested a "record", you've requested an aggregation of fields, bypassing the AR model - why on Earth (and how?) would AR be able to take over again without you explaining to it what the fields you selected represent? AR doesn't know about any error, because the fubar query that it generates *does* run and get reported as successful by the DB. > r.readonly? returns nil as well... Again, why would it not? Has anything you've done made a call to "r.readonly!" ? > I'm certain that I can create a new, empty AnalysisDatum records and > copy the fields from the records returned by the query, If you want a *new* AnalysisDatum record saved, you're gonna have to do something to populate a new record. You might be able to use the Marshal.load(Marshal.dump(foo)) method, or by cloning, but it may just come down to a new AnalysisDatum.create(:foo => :bar) > but I still want to be convinced this is not a bug. Convinced yet? :-) -- 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.