Forum: Ruby on Rails ActiveRecord problems with 4.1.13

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
hgs (Guest)
on 2005-11-12 12:49
(Received via mailing list)
I have MySQL 4.1.13, Rails 0.13.1, and Ruby 1.8.2
on Sun Sparc Solaris9

I'm getting, with warnings on:

pnumber is "00149246       "
exception is You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '"00149246       "  LIMIT 1' at line 1
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/validations.rb:687:
warning: instance variable @errors not initialized
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/callbacks.rb:336:
warning: instance variable @cse_modules not initialized
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/callbacks.rb:336:
warning: instance variable @cse_modules not initialized

Duplicate entry '"00149246       "' for key 2, Duplicate entry
'"00149246       "' for key 2
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/connection_adapters/mysql_adapter.rb:119:in
`query'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/connection_adapters/mysql_adapter.rb:119:in
`execute'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/connection_adapters/mysql_adapter.rb:113:in
`insert'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/base.rb:1155:in
`create_without_callbacks'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/callbacks.rb:261:in
`create_without_timestamps'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/timestamp.rb:30:in
`create'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/base.rb:1139:in
`create_or_update_without_callbacks'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/callbacks.rb:249:in
`create_or_update'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/base.rb:965:in
`save_without_validation'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/validations.rb:650:in
`save_without_transactions'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/transactions.rb:128:in
`save'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/transactions.rb:128:in
`transaction'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/transactions.rb:93:in
`transaction'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/transactions.rb:120:in
`transaction'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/transactions.rb:128:in
`save'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/validations.rb:656:in
`save!'
populate_tables2.rb:444:in `update_database'
populate_tables2.rb:434:in `each'
/usr/local/lib/ruby/1.8/set.rb:189:in `each_key'
/usr/local/lib/ruby/1.8/set.rb:189:in `each'
populate_tables2.rb:434:in `update_database'
populate_tables2.rb:489
neelix hgs 58 %>

And my update databases method looks like this:


    def update_database
      @students.each do |student|
        puts "pnumber is #{student.pnumber}"
        begin
          orig_student = Student.find_first(:pnumber => student.pnumber)
          puts "orig_student.pnumber is #{orig_student.pnumber}"
        rescue Exception => e
          puts "exception is #{e}"
          orig_student = nil
        end
        if orig_student.nil? # i.e. nothing found
          student.save!
        else
          orig_student.update_attributes(
                                    :surname => student.surname,
                                    :birth_dt => student.birth_dt,
                                    :picture => student.picture,
                                    :coll_status => student.coll_status
                                   )
        end
      end
      @cse_modules.each do |cse_module|
        orig_cse_module = CseModule.find_first(:aos_code =>
cse_module.aos_code) rescue nil
        if orig_cse_module.nil?
          cse_module.save!
        else
          orig_cse_module.update_attributes(
                                            :dept_code =>
cse_module.dept_code,
                                            :aos_type =>
cse_module.aos_type,
                                            :full_desc =>
cse_module.full_desc
                                           )
        end
      end
      # This next line should sort out the join table.
      @student_modules.each do |student, modules|
        the_student = Student.find_first(:pnumber => student.pnumber)
        modules.each do |cse_module|
          the_cse_module = CseModule.find_first(:aos_code =>
cse_module.aos_code)
          the_cse_module.students << the_student
        end
      end
    end


Questions I have about this:

How can the syntax be wrong?  Do I need to update rails? I'm reluctant
to do so given the number of problem reports relating to 0.14.x  Maybe
update MySQL?

Is this the correct way to sort out join tables?

        Thank you,
        Hugh
hgs (Guest)
on 2005-11-12 12:49
(Received via mailing list)
I think I've solved this now:
On Tue, 8 Nov 2005, Hugh Sasse wrote:

>
> I have MySQL 4.1.13, Rails 0.13.1, and Ruby 1.8.2
> on Sun Sparc Solaris9
>
> I'm getting, with warnings on:
>
> pnumber is "00149246       "
> exception is You have an error in your SQL syntax; check the manual that corresponds to 
your MySQL server version for the right syntax to use near '"00149246       "  LIMIT 1' at 
line 1
[...]
> And my update databases method looks like this:
>
>
>     def update_database
>       @students.each do |student|
>         puts "pnumber is #{student.pnumber}"
>         begin
>           orig_student = Student.find_first(:pnumber => student.pnumber)

Should have been (:conditions => ["pnumber = ?", student.pnumber])
       or find_pnumber(student.pnumber)...


        Hugh
This topic is locked and can not be replied to.