# Relationship between Tables

**URL:** <https://www.ruby-forum.com/t/relationship-between-tables/92875>\
**Category:** Rails\
**Created:** [April 25, 2007, 9:19am UTC](https://www.ruby-forum.com/t/relationship-between-tables/92875 "2007-04-25T09:19:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Linus\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@Linus\_M](https://www.ruby-forum.com/u/Linus_M)\
**Post date:** [April 25, 2007, 9:19am UTC](https://www.ruby-forum.com/t/relationship-between-tables/92875/1 "2007-04-25T09:19:45Z")

</div>

Hello,

how is it possible to specify a “has\_one” attribut in an one-to-many  
relationship?  
This code doesn’t work:

class Order \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :invoices  
has\_one :most\_recent\_invoice, :order =\> ‘date DESC’  
end

Thanks and best regards,  
Linus

---

<div class="post-metadata">

**Author:** ![Linus\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@Linus\_M](https://www.ruby-forum.com/u/Linus_M)\
**Post date:** [April 25, 2007, 4:48pm UTC](https://www.ruby-forum.com/t/relationship-between-tables/92875/2 "2007-04-25T16:48:23Z")

</div>

For this type of question you need to include the migrations for both  
tables for anyone to be able to help you, and the invoice class as  
well.

Do you have an order\_id in the invoice table for the has one relation?  
Does the invoice have a belongs\_to :order association defined?

Michael

On Apr 25, 12:18 am, Linus M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Linus\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@Linus\_M](https://www.ruby-forum.com/u/Linus_M)\
**Post date:** [April 26, 2007, 10:31am UTC](https://www.ruby-forum.com/t/relationship-between-tables/92875/3 "2007-04-26T10:31:25Z")

</div>

That is the problem. I have no order\_id in the invoice table.  
I have only the relationship table invoices\_orders with the foreign  
keys.

Class Inoice:

class Invoice \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :orders  
end

Linus

---

<div class="post-metadata">

**Author:** ![Linus\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@Linus\_M](https://www.ruby-forum.com/u/Linus_M)\
**Post date:** [April 26, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/relationship-between-tables/92875/4 "2007-04-26T21:35:27Z")

</div>

Hi,

I would extend the habtm association:

class Order \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :invoices do  
def most\_recent  
find :first, :order =\> “date DESC”  
end  
end  
end

Simon

---

<div class="post-metadata">

**Author:** ![Linus\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@Linus\_M](https://www.ruby-forum.com/u/Linus_M)\
**Post date:** [May 3, 2007, 2:08pm UTC](https://www.ruby-forum.com/t/relationship-between-tables/92875/5 "2007-05-03T14:08:09Z")

</div>

Hi Simon!

That’s a great solution! Thank you:-)

Linus
