Legacy db connection

Hello All. I need to connect to an older db that doesn’t follow the
rails naming structure. I can establish a connection ok and read data
but am unable to join two tables together within that database.
This is what I have:
Legacy DB tables:

mysql> describe frk_project;
±------------±----------------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±------------±----------------------±-----±----±--------±---------------+
| projectId | mediumint(8) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(120) | NO | | | |
| description | text | NO | | | |
±------------±----------------------±-----±----±--------±---------------+

mysql> describe frk_item;
±-----------------±----------------------±-----±----±-----------±---------------+
| Field | Type | Null | Key |Default|Extra
±-----------------±----------------------±-----±----±-----------±---------------+
| itemId | int(10) unsigned | NO | PRI | NULL |auto_increment
| projectId | mediumint(8) unsigned | NO | MUL | 0
| title | varchar(255) | NO | |
| description | text | NO | |

Rails Models:

class Freak_Item < ActiveRecord::Base
ActiveRecord::Base.establish_connection(
:adapter => “mysql”,
:encoding => “utf8”,
:username => “root”,
:database => “taskfreak”,
:socket => “/var/lib/mysql/mysql.sock”
)
self.table_name = ‘frk_item’
self.primary_key = ‘itemId’
belongs_to :freak_project
end

class Freak_Project < ActiveRecord::Base
ActiveRecord::Base.establish_connection(
:adapter => “mysql”,
:encoding => “utf8”,
:username => “root”,
:database => “taskfreak”,
:socket => “/var/lib/mysql/mysql.sock”
)
self.table_name = ‘frk_project’
self.primary_key = ‘projectId’
has_many :freak_items, :foreign_key => “projectId”
end

Am I missing something in the models ?
Thanks, any help appreciated…Bill

Siddick E. wrote:

Try with :class_name option in has_many method.

has_many :freak_items, :foreign_key => “projectId”, :class_name =>
“Freak_Item”

Thanks - didn’t work though :S

Try with :class_name option in has_many method.

has_many :freak_items, :foreign_key => “projectId”, :class_name =>
“Freak_Item”

E. Litwin wrote:

Are you getting an error message that you can post?

On May 29, 2:24�pm, Bill McGuire [email protected]

This is what i get when running via console - thanks

ruby script/console
Loading development environment (Rails 2.1.2)

a=Freak_Item.find(:first)
=> #<Freak_Item itemId: 1, projectId: 1, itemParentId: 0, priority: 3,
context:
“3”, title: “This is a test”, description: “some description”,
deadlineDate: “20
09-05-27”, expectedDuration: 0, showInCalendar: false, showPrivate:
true, member
Id: 8, authorId: 8>

b=Freak_Project.find(:first)
=> #<Freak_Project projectId: 1, name: “Testing”, description: “”>

a.freak_project.name
LoadError: Expected /home/admin/ie/app/models/freak_project.rb to define
FreakPr
oject

Are you getting an error message that you can post?

On May 29, 2:24 pm, Bill McGuire [email protected]

On Jun 1, 7:10 pm, Bill McGuire [email protected]
wrote:

b=Freak_Project.find(:first)

=> #<Freak_Project projectId: 1, name: “Testing”, description: “”>

a.freak_project.name

LoadError: Expected /home/admin/ie/app/models/freak_project.rb to define
FreakPr
oject

That looks like it was trying to find a class called FreakProject and
you only have Freak_Project. Use the :class_name option to belongs_to/
has_many.

Fred

Is there any reason you need the underscores in your model names?
i.e. Try naming it FreakProject instead of Freak_Project if possible.

On Jun 1, 11:10 am, Bill McGuire [email protected]

E. Litwin wrote:

Is there any reason you need the underscores in your model names?
i.e. Try naming it FreakProject instead of Freak_Project if possible.

On Jun 1, 11:10�am, Bill McGuire [email protected]

Thanks E and Frederick but no luck. Not really sure what’s gone wrong.

Frederick C. wrote:

On Jun 4, 12:03�am, Bill McGuire [email protected]
wrote:

E. Litwin wrote:

Is there any reason you need the underscores in your model names?
i.e. Try naming it FreakProject instead of Freak_Project if possible.

On Jun 1, 11:10 am, Bill McGuire [email protected]

Thanks E and Frederick but no luck. Not really sure what’s gone wrong.

So what did you try and with what results ?

Fred

Got it to work. Can’t believe i missed this. Foreign key was missing in
one model and was not foreign in the other (it was same as primary).

class Freak_Item < ActiveRecord::Base
ActiveRecord::Base.establish_connection(
:adapter => “mysql”,
:encoding => “utf8”,
:username => “root”,
:database => “taskfreak”,
:socket => “/var/lib/mysql/mysql.sock”
)
self.table_name = ‘frk_item’
self.primary_key = ‘itemId’
belongs_to :freak_project <<<< WAS MISSING THE FOREIGN_KEY
end

class Freak_Project < ActiveRecord::Base
ActiveRecord::Base.establish_connection(
:adapter => “mysql”,
:encoding => “utf8”,
:username => “root”,
:database => “taskfreak”,
:socket => “/var/lib/mysql/mysql.sock”
)
self.table_name = ‘frk_project’
self.primary_key = ‘projectId’
has_many :freak_items, :foreign_key => “projectId” <<<< WRONG
FOREIGN_KEY - NEEDS TO BE itemId
end

On Jun 4, 12:03 am, Bill McGuire [email protected]
wrote:

E. Litwin wrote:

Is there any reason you need the underscores in your model names?
i.e. Try naming it FreakProject instead of Freak_Project if possible.

On Jun 1, 11:10 am, Bill McGuire [email protected]

Thanks E and Frederick but no luck. Not really sure what’s gone wrong.

So what did you try and with what results ?

Fred