2013-03-05 31 views
0

我正在爲我的大學創建註冊系統。獲取關聯以顯示其所有所有模型的問題

我有兩個模型,一個是Register,另一個是Student。 一個Register有很多Students,我已經描繪了在各自.rb文件如下:

class Student < ActiveRecord::Base 
belongs_to :register 
attr_accessible :first_name, :last_name, :university_id 

class Register < ActiveRecord::Base 
    belongs_to :student 
    belongs_to :module_class 
    belongs_to :event 
    has_many :students 
    attr_accessible :date, :present, :time_of_arrival, :student_id, :module_class_id, :event_id 

的表是這樣的,從schema.rb文件:

create_table "registers", :force => true do |t| 
    t.integer "student_id" 
    t.boolean "present" 
    t.integer "module_class_id" 
    t.integer "event_id" 
    t.time  "time_of_arrival" 
    t.date  "date" 
    t.datetime "created_at",  :null => false 
    t.datetime "updated_at",  :null => false 
    end 

    add_index "registers", ["event_id"], :name => "index_registers_on_event_id" 
    add_index "registers", ["module_class_id"], :name => "index_registers_on_module_class_id" 
    add_index "registers", ["student_id"], :name => "index_registers_on_student_id" 

我現在的問題是,我希望能夠查看註冊表中的學生列表,以便將其標記爲存在或不存在(例如,在register/1中)。這是怎麼看起來像目前: enter image description here

然而,在此時此刻,我只能一個學生分配到寄存器,並出現Present複選框(這是register/new頁)Printscreen of New Register page

register指數本身看起來是這樣的: enter image description here

我使用simple_form寶石的形式,在情況下提供了更多的線索。

編輯:增加了圖表從RubyMine的顯示依賴關係: enter image description here

+1

'ModuleClass'是紅寶石中特別不幸的類名... – PinnyM 2013-03-05 16:00:08

+0

圖片不幫助我們調試您的代碼。顯示學生的輸出代碼。顯示保存學生的代碼(以證明你正在正確地建立關聯)。 – deefour 2013-03-05 16:00:48

+0

你到底需要什麼@Deefour?並且是@PinnyM,非常不幸,因爲'Module'和'Class'已經被Ruby定義了,所以我不得不想出這個 – omarArroum 2013-03-05 17:40:55

回答

0

一個註冊有很多學生你說,那你爲什麼使用belongs_to :student並在登記表中加入一個student_id列?

+0

好吧,我改變了這個,一定忘了把它取出來。 我仍然無法選擇一個學生的整個列表成爲一個註冊的一部分 – omarArroum 2013-03-06 11:22:20

+0

你能顯示相關的控制器和查看代碼嗎? – Dimitri 2013-03-06 12:16:47

+0

這是'register/_form.html.erb'文件 '<%= simple_form_for(@register)do | f | %> <%= f.error_notification%>

<%= f.association :students %> <%= f.input :present, :autofocus => true %> <%= f.association :module_class %> <%= f.association :event %> <%= f.input :time_of_arrival %> <%= f.input :date %>
<%= f.button :submit %>
<% end %>' 而對於在'show'學生視圖部分是: '

學生: <%= Student.find_by_id( @ register.student_id).name%>

' – omarArroum 2013-03-06 12:59:43

相關問題