public
Last active

  • Download Gist
new_student.html.erb
HTML+ERB
1 2
<%= f.text_field :pending_room_number, :value => @student.room_number %>
<%= f.collection_select :residence_id, Residence.all, :id, :name %>
room.rb
Ruby
1 2 3 4
class Room < ActiveRecord::Base
belongs_to :residence
has_many :students
end
student.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class Student < ActiveRecord::Base
belongs_to :room
delegate :number, :residence, :to => :room, :prefix => true, :allow_nil => true
attr_accessor :pending_room_number, :residence_id
 
before_save :assign_room
 
def assign_room
self.room = if room = Room.find_by_number_and_residence_id(pending_room_number, residence_id)
room
else
Room.create! { :number => pending_room_number, :residence_id => residence_id}
end unless self.room.present?
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.