Can we manually add one entry into dynamic drop down list, If then how in Ruby On Rails -
i have student, student_parent , address_detail table want add student , it's parent(father , mother) address in address_detail table, have folowing relation between tables
student:- has_many :student_parents has_many :address_details
student_parent: belongs_to :student has_many :address_detail
address_detail: belongs_to :student belongs_to :student_parent
in address_detail form have drop down father , mother want add student entry manually how can do, here my address_detail form``
<%= simple_form_for @address_detail, :html => { :class => 'form-horizontal' } |f| %> <div class="control-group"> <label class = "control-label"> address correspond <abbr title="required">*</abbr></label> <div class="controls"> <%= f.collection_select(:student_parent_id, student_parent_relation_collection , :id, :relation_to_student, {:prompt => true}, :required =>true )%> <%= f.hidden_field :student_id, :value => current_student_id %> </div> </div>
.... .....
here helper method address_correspond_to drop down method
# return collection of parent relation student
def student_parent_relation_collection
if current_user != nil student = student.find_all_by_user_id(current_user.id) logger.info "student_is = #{student}" if student != nil return studentparent.find_all_by_student_id(student) end end
end
current output father mother
i want out student father mother
class addressdetail < activerecord::base def student_and_parents [student, student_parent] end end
then, in view can either refer
address_detail.student_and_parents
or in user
class user < activerecord::base has_many :students has_many :student_parents, :through => :students def students_and_parents [students, student_parents].compact.flatten end end
you can refer
current_user.try(:students_and_parents)
Comments
Post a Comment