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 -- Posted via http://www.ruby-forum.com/.
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 -%> <p> <%= arrival.date %> ... <br/> <%= departure.date %> </p> <% end -%> Another way of doing things might be to Model from a class - ''Flight'' which has one arrival and one departure. Cheers, Jan -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060804/23f5bfc6/attachment-0001.html
On 8/4/06, nuno <nomail@novalidatall991.com> wrote:> 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)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
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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060804/d2288ad8/attachment.html
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. -- Posted via http://www.ruby-forum.com/.
On 04/08/06, nuno <nomail@novalidatall991.com> 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. -- Cheers, Hasan Diwan <hasan.diwan@gmail.com> -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060805/5fd60907/attachment.html
Hasan Diwan wrote:> On 04/08/06, nuno <nomail@novalidatall991.com> 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... -- Posted via http://www.ruby-forum.com/.
On 05/08/06, nuno <nomail@novalidatall991.com> 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 %> <tr><td><%=h arrival.send (''bleh'') %></td></tr> <tr><td><%=h departure.find(:first, :id => arrival.movement_id) %></td></tr> <% end %> Yes, it''s probably not syntactically correct, but you get the idea. -- Cheers, Hasan Diwan <hasan.diwan@gmail.com> -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060806/6d7f4e22/attachment-0001.html
Hasan Diwan 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 !! -- Posted via http://www.ruby-forum.com/.