# Legacy MySQL database and relationships

**URL:** <https://www.ruby-forum.com/t/legacy-mysql-database-and-relationships/163239>\
**Category:** Rails\
**Created:** [March 18, 2009, 2:50pm UTC](https://www.ruby-forum.com/t/legacy-mysql-database-and-relationships/163239 "2009-03-18T14:50:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![martins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@martins](https://www.ruby-forum.com/u/martins)\
**Post date:** [March 18, 2009, 2:50pm UTC](https://www.ruby-forum.com/t/legacy-mysql-database-and-relationships/163239/1 "2009-03-18T14:50:33Z")

</div>

Hi,

I´m able to get the data I want with AchievoPerson.find and  
AchievoProject.find, but the has\_and\_belongs\_to\_many associations wont  
work.  
Any suggestions or links to posts that might help me out would be  
highly appreciated!

p = AchievoPerson.find(:first) # =\> Find a person which is connected  
to projects in the project\_person table

> > p.projects  
> > NameError: uninitialized constant AchievoPerson::AchievoProjects  
> > from …/rails/activesupport/lib/active\_support/dependencies.rb:  
> > 492:in `const_missing' from ../rails/activerecord/lib/active_record/base.rb:1909:in`compute\_type’  
> > from …/rails/activerecord/lib/active\_record/reflection.rb:129:in  
> > `send' from ../rails/activerecord/lib/active_record/reflection.rb:129:in`klass’  
> > from …/rails/activerecord/lib/active\_record/reflection.rb:137:in  
> > `quoted_table_name' from ../rails/activerecord/lib/active_record/associations/ has_and_belongs_to_many_association.rb:82:in`construct\_sql’  
> > from …/rails/activerecord/lib/active\_record/associations/  
> > association\_collection.rb:8:in `initialize' from ../rails/activerecord/lib/active_record/associations.rb:1128:in`new’  
> > from …/rails/activerecord/lib/active\_record/associations.rb:1128:in  
> > `projects’

class AchievoProject \< ActiveRecord::Base  
establish\_connection :achievo  
self.abstract\_class = true  
set\_primary\_key “id”

def self.table\_name() “project” end  
def get\_active  
AchievoProject.find(:all, :conditions =\>{:status =\> ‘active’} )  
end

has\_and\_belongs\_to\_many :persons, :class\_name =\>  
“AchievoPersons”, :join\_table =\> “project\_person”, :foreign\_key =\>  
“projectid”  
end

class AchievoPerson \< ActiveRecord::Base  
establish\_connection :achievo  
set\_primary\_key “id”

def self.table\_name() “person” end  
has\_and\_belongs\_to\_many :projects, :class\_name =\>  
“AchievoProjects”, :join\_table =\> “project\_person”, :foreign\_key =\>  
“personid”  
end

#—

# mysql\> describe project\_person;

# ±----------±--------±-----±----±--------±------+

# | Field | Type | Null | Key | Default | Extra |

# ±----------±--------±-----±----±--------±------+

# | projectid | int(10) | NO | PRI | 0 | |

# | personid | int(10) | NO | PRI | 0 | |

# | role | int(10) | NO | PRI | 0 | |

# ±----------±--------±-----±----±--------±------+

#—

–  
Best Regards,  
Martin S.

---

<div class="post-metadata">

**Author:** ![martins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@martins](https://www.ruby-forum.com/u/martins)\
**Post date:** [March 18, 2009, 2:53pm UTC](https://www.ruby-forum.com/t/legacy-mysql-database-and-relationships/163239/2 "2009-03-18T14:53:29Z")

</div>

On Mar 18, 9:42 am, martins [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

This

> class AchievoProject \< ActiveRecord::Base

and this

> has\_and\_belongs\_to\_many :projects, :class\_name =\>  
> “AchievoProjects”, :join\_table =\> “project\_person”, :foreign\_key =\>  
> “personid”

must match: if you tell active record that the class is called  
AchievoProjects but it’s actually called AchievoProject then it’s not  
going to fly.

Fred
