Multiple Ruby on Rails Model Relationship -
i trying access location details thru shipment model has origin , destination.
i access location information thru call this:
shipment.find(###).origin -> :id => "###", :cs => "springfield, mo", :latitude => "#####", :longitude => "####" shipment.find(###).dest -> :id => "###", :cs => "springfield, mo", :latitude => "#####", :longitude => "####"
i have following setup
shipment.rb:
class shipment < activerecord::base belongs_to :user has_many :vehicles, dependent: :destroy validates :origin_id, :dest_id, :presence => true attr_accessible :origin_id, :dest_id, (more attributes...) end
location.rb
class location < activerecord::base attr_accessible :cs, :latitude, :longitude geocoded_by :cs after_validation :geocode, :if => :cs_changed? has_many :shipments, :foreign_key => :origin_id has_many :shipments, :foreign_key => :dest_id end
i think this:
has_one :origin, :class => "location", :origin_id
or
scope :origin, -> (:origin_id) { location.find(:origin_id)}
it can be
class shipment < activerecord::base ... belongs_to :origin, class: 'location', foreign_key: 'origin_id' belongs_to :dest, class: 'location', foreign_key: 'dest_id' end
for older rails
class shipment < activerecord::base ... belongs_to :origin, class_name: 'location', foreign_key: 'origin_id' belongs_to :dest, class_name: 'location', foreign_key: 'dest_id' end
then origin location
shipment.find(###).origin
then destination location
shipment.find(###).dest
Comments
Post a Comment