Legacy MySQL database and relationships


#1

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:incompute_type’
from …/rails/activerecord/lib/active_record/reflection.rb:129:in
send' from ../rails/activerecord/lib/active_record/reflection.rb:129:inklass’
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:inconstruct_sql’
from …/rails/activerecord/lib/active_record/associations/
association_collection.rb:8:in initialize' from ../rails/activerecord/lib/active_record/associations.rb:1128:innew’
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.


#2

On Mar 18, 9:42 am, martins 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