Skip to content

Instantly share code, notes, and snippets.

@Aeon
Created March 18, 2009 08:59
Show Gist options
  • Save Aeon/81010 to your computer and use it in GitHub Desktop.
Save Aeon/81010 to your computer and use it in GitHub Desktop.
class Segment < ActiveRecord::Base
has_many :street_segments
has_many :streets, :through => :street_segments, :source => :street
after_save :ensure_primary
def ensure_primary
if self.streets.size > 0 && self.primary_street.nil?
# set the primary street attribute in association (in reality more criteria than just being 'first', but whatever)
primary_street = self.street_segments.first
primary_street.make_primary
end
end
def primary_street
self.street_segments.find_all { |st| st.prim == "1" }.first.street
end
end
class StreetSegment < ActiveRecord::Base
set_table_name "segments_streets"
belongs_to :segment
belongs_to :street
def make_primary
self.prim = 1
self.save!
end
end
class Street < ActiveRecord::Base
has_many :addresses
has_many :street_segments
has_many :segments, :through => :street_segments, :source => :segment
end
class SegmentsController < ApplicationController
def create
@segment = Segment.new(params[:segment])
@possible_streets = Street.find_by_sql "SELECT s.* FROM streets AS s ... WHERE ... GROUP BY s.id"
@segment.streets = @possible_streets
@segment.save!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment