I need an idea!

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

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

On 8/4/06, nuno [email protected] 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

– James

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.

On 04/08/06, nuno [email protected] 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.

Hasan D. wrote:

On 04/08/06, nuno [email protected] 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…

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

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

On 05/08/06, nuno [email protected] 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs