# Legacy Join

**URL:** <https://www.ruby-forum.com/t/legacy-join/91881>\
**Category:** Rails\
**Created:** [April 13, 2007, 2:05am UTC](https://www.ruby-forum.com/t/legacy-join/91881 "2007-04-13T02:05:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [April 13, 2007, 2:05am UTC](https://www.ruby-forum.com/t/legacy-join/91881/1 "2007-04-13T02:05:45Z")

</div>

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”

- 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!

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [April 13, 2007, 6:31am UTC](https://www.ruby-forum.com/t/legacy-join/91881/2 "2007-04-13T06:31:45Z")

</div>

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.
