Legacy Join


#1

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!


#2

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.