# I need an idea!

**URL:** <https://www.ruby-forum.com/t/i-need-an-idea/101418>\
**Category:** Rails\
**Created:** [August 4, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/i-need-an-idea/101418 "2006-08-04T19:50:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [August 4, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/i-need-an-idea/101418/1 "2006-08-04T19:50:46Z")

</div>

Hello here, I need to find an idea to solve a problem

I manage Events which are of kind :arrival or :departure  
At first the two kind of events was unrelated but my client want to see  
the :departure belonging to the :arrival and vice versa.

I’m searching for a solution to implement this ‘link’. I’ve thought  
about an integer column containing an uniq id for the same pairs of  
Events but I don’t like this solution because I don’t know how to  
display the related event in a for loop

Events must be displayed ordered by date with for each (:arrival, the  
corresponding :departure)

Eg.

1. 12/04/2006 - 99AABB - Arrival  
20/05/2006 - 99AABB - Departure
2. 15/04/2006 - 88CCAA - Arrival  
23/04/2006 - 88CCAA - Departure

Any idea is welcome, I’m stuck !! (And of course I would avoid running a  
query for each line displayed )

Thanks

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [August 4, 2006, 8:06pm UTC](https://www.ruby-forum.com/t/i-need-an-idea/101418/2 "2006-08-04T20:06:53Z")

</div>

Hi Nuno,

if I get you right you’ve got an arrival (let’s say from a plane) on  
your  
airport and a departure of that plane.

If that’s true, then a plane can’t arrive twice without being departured  
as  
well as a plane can’t departure without arriving earlier.

Wouldn’t than the natural way of modelling be:

class Arrival  
has\_one :departure, :include =\> :departure  
end

class Departure  
belongs\_to :arrival  
end

This way you should be able to iterate over arrivals to construct a list  
like yours where the departures are eager loaded

@arrivals = Arrival.find(:all) # or limit and or paginate

\<% for arrival in @arrivals -%\>

\<%= arrival.date %\> ...   
 \<%= departure.date %\>

\<% end -%\>

Another way of doing things might be to Model from a class - ‘Flight’  
which  
has one arrival and one departure.

Cheers,  
Jan

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [August 4, 2006, 8:12pm UTC](https://www.ruby-forum.com/t/i-need-an-idea/101418/3 "2006-08-04T20:12:53Z")

</div>

On 8/4/06, nuno [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Events must be displayed ordered by date with for each (:arrival, the  
> corresponding :departure)

This sounds like a special case of the “has\_many :through”  
relationship, where you really only “have one”.

If you could have, for example, a “trips” table that stored an arrival  
id and a departure id, you’d be able to easily relate the two. Plus,  
you could then sort a list of Trip objects by dates from either  
related table.

[http://wiki.rubyonrails.org/rails/pages/ThroughAssociations](http://wiki.rubyonrails.org/rails/pages/ThroughAssociations)

– James

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [August 5, 2006, 4:31am UTC](https://www.ruby-forum.com/t/i-need-an-idea/101418/4 "2006-08-05T04:31:57Z")

</div>

Thanks a lot for your help !

I dunno if I choosed the right way but following more or less your  
advices I did this :

class ArrivalDeparture \< ActiveRecord::Base  
belongs\_to :vehicle  
has\_one :arrival, :class\_name =\> ‘Movement’, :dependant =\> :destroy  
has\_one :departure, :class\_name =\> ‘Movement’, :dependant =\> :destroy  
end

And

class Movement \< ActiveRecord::Base  
belongs\_to :ArrivalDeparture

# bunch of properties here including a :kind\_of property feeded with

symbols

# :departure or :arrival

end

And now I do  
ArrivalDeparture .find(:all,  
:include =\> [:arrival, :departure],  
:conditions =\> [“movements.activ = ? AND movements.statut = ? AND  
movements.date\_programmed BETWEEN ? AND ?”, true, :not\_handled,  
Date.today, Date.today + some\_days],  
:order =\> “movements.date\_programmed”)

Unit tests seems to be OK, I’ll will check later if the relationship for  
special cases like ArrivalDeparture with a departure but NO arrival is  
correctly handled.

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [August 5, 2006, 11:39pm UTC](https://www.ruby-forum.com/t/i-need-an-idea/101418/5 "2006-08-05T23:39:39Z")

</div>

On 04/08/06, nuno [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Unit tests seems to be OK, I’ll will check later if the relationship for  
> special cases like ArrivalDeparture with a departure but NO arrival is  
> correctly handled.

Strictly speaking, I would think these “special cases” really should be  
unit  
tests.

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [August 6, 2006, 3:45am UTC](https://www.ruby-forum.com/t/i-need-an-idea/101418/6 "2006-08-06T03:45:56Z")

</div>

Hasan D. wrote:

> On 04/08/06, nuno [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Unit tests seems to be OK, I’ll will check later if the relationship for  
> > special cases like ArrivalDeparture with a departure but NO arrival is  
> > correctly handled.
> 
> Strictly speaking, I would think these “special cases” really should be  
> unit  
> tests.

You are right and I think like you (I meant checking later = writing  
more unit test)

BTW : there is something wrong with the solution I have choosen  
How the has\_one :arrival and :departure parameter can be related with  
one movement ? How RoR can associate a particular Movement instance  
with it ? I presume I should add a :condition eg. :  
has\_one :departure, :class\_name =\> ‘Movement’, :dependant =\> :destroy,  
:conditions =\> [‘kind = ?’, :departure]

I have the feeling that there’s something wrong somewhere…

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [August 4, 2006, 8:45pm UTC](https://www.ruby-forum.com/t/i-need-an-idea/101418/7 "2006-08-04T20:45:56Z")

</div>

Hi Nuno,

now I’m getting things. You’ve got one model ‘events’, which might be of  
kind ‘arrival’ or ‘departure’. IMHO you can model this by using  
single-table-inheritance (STI) where the type column is either ‘arrival’  
or  
‘departure’. Then on this table you could model the has\_one, belongs\_to  
relationship as a self-referring one. There are examples for such  
self-referential relationships in RailsRecipes and on the wiki.

You could of course model this relationship :through another model as  
described by james, especially if you are in need of a fine grained  
control  
over the relationship itself.

Cheers,  
Jan

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [August 7, 2006, 10:58am UTC](https://www.ruby-forum.com/t/i-need-an-idea/101418/8 "2006-08-07T10:58:02Z")

</div>

Hasan D. wrote:

> Yes, it’s probably not syntactically correct, but you get the idea.

Yes, this is far easier than what I was trying to do…

More or less, following your idea now I

arrivals = Movements.find(:all, :conditions =\> [“mov\_type = ?”,  
:departure], :order =\> “date\_programmed”]

for arrival in arrivals

# display arrival

dep = Movement.find(:first, :conditions =\> [“id\_pair = ?”,  
arrival.id\_pair])  
end

Each pair of arrival / departure is identified by an unique id

Thanks !!

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [August 6, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/i-need-an-idea/101418/9 "2006-08-06T19:53:12Z")

</div>

On 05/08/06, nuno [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> BTW : there is something wrong with the solution I have choosen  
> How the has\_one :arrival and :departure parameter can be related with  
> one movement ? How RoR can associate a particular Movement instance  
> with it ? I presume I should add a :condition eg. :  
> has\_one :departure, :class\_name =\> ‘Movement’, :dependant =\> :destroy,  
> :conditions =\> [‘kind = ?’, :departure]

Could you not have a movement\_id foreign key in your :arrival or  
:departure?  
When you display, you’d do something like:  
\<% for arrival in @arrivals %\>

\<%=h arrival.send ('bleh') %\> \<%=h departure.find(:first, :id =\> arrival.movement\_id) %\> \<% end %\>

Yes, it’s probably not syntactically correct, but you get the idea.
