Hi all, I try to create a little game, my first application in Ruby and Rails. And I''ve problems understanding polymorphic associations between models. I have a model called "Travel". Two models, "Character" and "Truck" can participate in a travel. How should I setup the models ? I tried it this way: class Travel < ActiveRecord::Base has_many :travellers end class Traveller < ActiveRecord::Base belongs_to :travel belongs_to :travelable, :polymorphic => true end class Character < ActiveRecord::Base has_one :traveller, :as => :travelable end class Truck < ActiveRecord::Base has_one :traveller, :as => :travelable end Not only that it''s not working, it''s also not a very clean approach. I think that''s easy to explain for a more experienced developer than me, so please give me some hints. Thank you very much in advance and greetings from Austria Christoph --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
setup seems ok, assuming that Traveller is the poly model and has the columns travelable_id and travelable_type Otherwise it would be helpful, to know what is not working and what exactly you want to do here. (And why you think it''s not a clean approach) With games it''s especially difficult to guess your intentions. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Otherwise it would be helpful, to know what is not working > and what exactly you want to do here.Ok, Traveller has these columns: create_table "travellers", :force => true do |t| t.integer "travel_id" t.integer "travelable_id" t.integer "travelable_type" end When I do this in script/console Travel.last.travellers << Character.last I get the following error: ActiveRecord::AssociationTypeMismatch: Traveller(#16747500) expected, got Character(#16661940) (And why you think it''s not a clean approach) I think it''s not very clean, because I need an extra Traveller model. I would like to avoid this if it''s possible. The situation is the following: Both Characters and Trucks can travel. One Travel Record holds the from/to location and through a 1:n relationship either the participating characters/trucks. I hope my English is understandable. Greetings Christoph --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---