Hey, I''ve got a polymorphic relation and would like to create an object. How is it possible to determine whether its a ''car'' or a ''truck''? A person can owe cars and trucks. I could pass params like the car/truck id to the action but that doesn''t really help me. For now I do it like this but I don''t like it at all: person.cars = Car.find(params[:id]) if params[:type] == "Car" person.truck = Truck.find(params[:id]) if params[:type] == "Truck" This is really not how I want it. Does anyone knows how to solve that kind of problem? Thanks -- Posted via http://www.ruby-forum.com/.
I think that you should create some kind of an interface, for example Vehicle, like this: class Vehicle < ActiveRecord::Base belongs_to :person, :polymorphic => true end After that you should make the inherited models Car and Truck: class Car < Vehicle end class Truck < Vehicle end and finally define the person model: class Person < ActiveRecord::Base has_many :vehicles end Keep in mind that the Vehicle table should have a string column :type, where the class of the record is stored. With this models you can do this: p = Person.create(:name => "John") p.vehicles << Car.create(:model => "Fiat") p.vehicles << Truck.create(:model => "Mercedes") For more information: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html Does that help answer your question? --Valentin On Sep 22, 5:51 pm, Heinz Strunk <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hey, > > I''ve got a polymorphic relation and would like to create an object. How > is it possible to determine whether its a ''car'' or a ''truck''? > > A person can owe cars and trucks. I could pass params like the car/truck > id to the action but that doesn''t really help me. > > For now I do it like this but I don''t like it at all: > person.cars = Car.find(params[:id]) if params[:type] == "Car" > person.truck = Truck.find(params[:id]) if params[:type] == "Truck" > > This is really not how I want it. Does anyone knows how to solve that > kind of problem? > > Thanks > -- > Posted viahttp://www.ruby-forum.com/.
I''m not sure of what you need to do but I''ll assume that you just want to create an association between a person and a vehicle of type car or truck (or whatever). I am assuming that you have STI built into your vehicles table, with a column named ''type'' that stores the type of vehicle (''Car''/''Truck''/''Whatever''). If you just want to assign a vehicle to the person I don''t see a reason why you should know the type of vehicle it is. Based on your code you know the id of the vehicle. Why not do something simpler?: person.car = Vehicle.find(params[:id]) You might not know the type of vehicle it is but does it matter? And going even further, do you really need to retrieve the vehicle row? I guess that if you can''t trust the id beeing an honest value you should but if you could trust it, couldn''t you just do?: person.vehicle_id = params[:id] Maybe I am not understanding your need? On Sep 22, 10:51 am, Heinz Strunk <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hey, > > I''ve got a polymorphic relation and would like to create an object. How > is it possible to determine whether its a ''car'' or a ''truck''? > > A person can owe cars and trucks. I could pass params like the car/truck > id to the action but that doesn''t really help me. > > For now I do it like this but I don''t like it at all: > person.cars = Car.find(params[:id]) if params[:type] == "Car" > person.truck = Truck.find(params[:id]) if params[:type] == "Truck" > > This is really not how I want it. Does anyone knows how to solve that > kind of problem? > > Thanks > -- > Posted viahttp://www.ruby-forum.com/.
First of all thank you guys! I think what Valentin wrote is just what I was looking for. Person, Car, Truck was just an example. It''s a lot more complexe in my application and you can''t just merge these two different models into one and add a :type. I''ll do it like Valentin suggested. Thanks again! :) -- Posted via http://www.ruby-forum.com/.
Heinz Strunk wrote:> First of all thank you guys! I think what Valentin wrote is just what I > was looking for. Person, Car, Truck was just an example. It''s a lot more > complexe in my application and you can''t just merge these two different > models into one and add a :type. > > I''ll do it like Valentin suggested. Thanks again! :)It should be noted that Valentin suggested a STI solution (which is what I would have recommended) and your question resolved around polymorphic associations which is a different concept entirely. Polymorphic associations deal with association polymorphism where as STI deals primarily wih model polymorphism. hth ilan -- Posted via http://www.ruby-forum.com/.