# ActiveRecord problems with 4.1.13

**URL:** <https://www.ruby-forum.com/t/activerecord-problems-with-4-1-13/47524>\
**Category:** Rails\
**Created:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/activerecord-problems-with-4-1-13/47524 "2005-11-12T12:49:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hgs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e5b9ba/32.png) [@hgs](https://www.ruby-forum.com/u/hgs)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/activerecord-problems-with-4-1-13/47524/1 "2005-11-12T12:49:18Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![hgs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e5b9ba/32.png) [@hgs](https://www.ruby-forum.com/u/hgs)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/activerecord-problems-with-4-1-13/47524/2 "2005-11-12T12:49:20Z")

</div>

I think I’ve solved this now:  
On Tue, 8 Nov 2005, Hugh S. 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
```
