musty
May 29, 2009, 2:12am
1
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
musty
May 29, 2009, 11:24pm
2
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
musty
May 29, 2009, 9:24am
3
Try with :class_name option in has_many method.
has_many :freak_items, :foreign_key => “projectId”, :class_name =>
“Freak_Item”
musty
June 1, 2009, 8:10pm
4
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
musty
May 30, 2009, 2:13am
5
Are you getting an error message that you can post?
On May 29, 2:24 pm, Bill McGuire [email protected]
musty
June 1, 2009, 8:28pm
6
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
musty
June 1, 2009, 8:32pm
7
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]
musty
June 4, 2009, 1:03am
8
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.
musty
June 5, 2009, 12:50am
9
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
musty
June 4, 2009, 9:44am
10
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