Forum: Ruby on Rails legacy db connection

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.
E6ca8d60933425082ea975f795654919?d=identicon&s=25 Bill McGuire (wjm)
on 2009-05-29 02:12
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
60ddadf1a8a8ebac760e7e4cc1e342b3?d=identicon&s=25 Siddick Ebramsha (siddick)
on 2009-05-29 09:24
Try with :class_name option in has_many method.

  has_many :freak_items, :foreign_key => "projectId", :class_name =>
"Freak_Item"
E6ca8d60933425082ea975f795654919?d=identicon&s=25 Bill McGuire (wjm)
on 2009-05-29 23:24
Siddick Ebramsha 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
Ced56202e9039027cfc2da2f70457977?d=identicon&s=25 E. Litwin (Guest)
on 2009-05-30 02:13
(Received via mailing list)
Are you getting an error message that you can post?

On May 29, 2:24 pm, Bill McGuire <rails-mailing-l...@andreas-s.net>
E6ca8d60933425082ea975f795654919?d=identicon&s=25 Bill McGuire (wjm)
on 2009-06-01 20:10
E. Litwin wrote:
> Are you getting an error message that you can post?
>
> On May 29, 2:24�pm, Bill McGuire <rails-mailing-l...@andreas-s.net>

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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-06-01 20:28
(Received via mailing list)
On Jun 1, 7:10 pm, Bill McGuire <rails-mailing-l...@andreas-s.net>
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
Ced56202e9039027cfc2da2f70457977?d=identicon&s=25 E. Litwin (Guest)
on 2009-06-01 20:32
(Received via mailing list)
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 <rails-mailing-l...@andreas-s.net>
E6ca8d60933425082ea975f795654919?d=identicon&s=25 Bill McGuire (wjm)
on 2009-06-04 01:03
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 <rails-mailing-l...@andreas-s.net>

Thanks E and Frederick but no luck. Not really sure what's gone wrong.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-06-04 09:44
(Received via mailing list)
On Jun 4, 12:03 am, Bill McGuire <rails-mailing-l...@andreas-s.net>
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 <rails-mailing-l...@andreas-s.net>
>
> Thanks E and Frederick but no luck. Not really sure what's gone wrong.

So what did you try and with what results ?

Fred
E6ca8d60933425082ea975f795654919?d=identicon&s=25 Bill McGuire (wjm)
on 2009-06-05 00:50
Frederick Cheung wrote:
> On Jun 4, 12:03�am, Bill McGuire <rails-mailing-l...@andreas-s.net>
> 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 <rails-mailing-l...@andreas-s.net>
>>
>> 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
This topic is locked and can not be replied to.