Forum: Ruby on Rails Legacy MySQL database and relationships

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.
3b1daae9f655c5dba51ff1cdf03f1bef?d=identicon&s=25 martins (Guest)
on 2009-03-18 14:50
(Received via mailing list)
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 Stabenfeldt
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-18 14:53
(Received via mailing list)
On Mar 18, 9:42 am, martins <mar...@stabenfeldt.net> 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
This topic is locked and can not be replied to.