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)
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.
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.
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.
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…
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.
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 %>