2013-07-31 14 views
1

我有一個student,student_parent和address_detail表我想添加學生,並且它是父地址(父和母)在address_detail表中,我有我們可以手動添加一個條目到動態下拉列表中,如果那麼在Ruby On Rails中如何在Ruby on Rails中

學生表之間的關係如下因素: - 的has_many:student_parents 的has_many:address_details

Student_parent: belongs_to的:學生 的has_many:address_detail

address_detail: belongs_to的:學生 belongs_to的:student_parent

在address_detail形式我有一個下拉只有父親和母親,我想手動我該怎麼辦添加學生進入,這裏是我address_detail形式``

<%= simple_form_for @address_detail, :html => { :class => 'form-horizontal' } do |f| %> 
<div class="control-group"> 
    <label class = "control-label"> Address Correspond To <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> 

.... .....

這裏是我的Address_correspond_to Helper方法下拉法

# return collection of parent relation to student 

高清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 

電流輸出 父親 母親

我想出來 斯圖登牛逼 父親 母親

回答

0
Class AddressDetail < ActiveRecord::Base 

    def student_and_parents 
    [student, student_parent] 
    end 

end 

然後,在視圖中,您可以參考

address_detail.student_and_parents 

或用戶

Class User < ActiveRecord::Base 

    has_many :students 
    has_many :student_parents, :through => :students 

    def students_and_parents 
    [students, student_parents].compact.flatten 
    end 

end 

你可以參考

current_user.try(:students_and_parents) 
0
 
if current_user 
    student = Student.find_by_user_id(current_user.id) 
    return [student] + student.student_parents if student 
end