Forum: Ruby on Rails Legacy Join

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.
Ryan (Guest)
on 2007-04-13 04:05
Hello,

I have 2 legacy tables I want to join:

DB TABLES
Transaction_Details
Accounts

I want to join Transaction_Detail.Account with Accounts.Account.

I also want to be able to select Transaction_Details based on a field in
the Accounts table.  Is that possible? I'm thinking it should be.

I'd prefer to do it with Active Record, as opposed to find_by_sql.

I currently have:

MODELS
- transaction_detail.rb -
class Transaction_Detail < ActiveRecord::Base
  set_table_name "Transaction_Details"

  has_one  :account
end

- account.rb -

class Account < ActiveRecord::Base
  set_table_name "Accounts"

  has_many :transaction_details
end

CONTROLLER
TransactionDetail.find(:all) ... not sure what to do about the
:conditions here


Thanks!
Michael W. (Guest)
on 2007-04-13 08:31
(Received via mailing list)
Ryan wrote:
> I also want to be able to select Transaction_Details based on a field in
>
>
> CONTROLLER
> TransactionDetail.find(:all) ... not sure what to do about the
> :conditions here

Two things. First you can override the default primary key for a model
with self.primary_key in your model such as:

class Account < ActiveRecord::Base
   set_table_name "Accounts"
   set_primary_key "account"
# or
# self.primary_key = "account"

Second you can override the default column used for a join with
:foreign_key such as:

class Account < ActiveRecord::Base
   set_table_name "Accounts"
   set_primary_key "account"
# or
# self.primary_key = "account"

   has_many :transaction_details, :foreign_key => "account"


--
Michael W.
This topic is locked and can not be replied to.