On May 9, 10:02 am, Fearless Fool <[email protected]> wrote:
> If an active record is returned as a result of a find_by_sql() query,
> what must be true in order for it to be save-able?  Here's an example of
> it not working:
> =====
> require 'rubygems'
> require 'active_record'
> ActiveRecord::Schema.define do
>   create_table(:blurgs, :force => true) {|t| t.string :name }
> end
> class Blurg < ActiveRecord::Base
> end
>
> a = Blurg.create(:name => "tic")
> p "a.name = #{a.name}"
> p "Blurg.count => #{Blurg.count}"
>
> b = Blurg.find_by_sql('SELECT \'tac\' as name')
> p "b = #{b}"
> p "b.first.name = #{b.first.name}"
> p "b.first.save! => #{b.first.save!}"
> p "Blurg.count => #{Blurg.count}"
> =====
> which produces
> =====
> "a.name = tic"
> "Blurg.count => 1"
> "b = [#<Blurg name: \"tac\">]"
> "b.first.name = tac"
> "b.first.save! => true"
> "Blurg.count => 1"
> =====
> So even though b.first has a .name value, calling b.first.save! silently
> fails to store it in the database.
>
> Is there a way to detect whether a record can be saved or not?  Or at
> least give an error when you try to save it?
>

You didn't select the primary key value so rails will have done

update blugs set name = 'tac' where id is null

Whenever you use a customer select clause you have to be a little
careful about what you select.

Fred
> - ff
> --
> Posted viahttp://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 [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group 
> athttp://groups.google.com/group/rubyonrails-talk?hl=en.

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to