Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View new_student.html.erb
1 2
<%= f.text_field :pending_room_number, :value => @student.room_number %>
<%= f.collection_select :residence_id, Residence.all, :id, :name %>
View new_student.html.erb
1 2 3 4
class Room < ActiveRecord::Base
belongs_to :residence
has_many :students
end
View new_student.html.erb
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.