2012-03-28 73 views
0

這裏是新手問題。如何在導軌中創建相關模型

我有這些相互關聯的兩個型號:

class Relationship < ActiveRecord::Base 
    ... 
    attr_accessible :source_item_id, :target_item_id 
    belongs_to :target_item, :class_name => "Item" 
    belongs_to :source_item, :class_name => "Item" 
    belongs_to :user 
    ... 
end 

和:

class Item < ActiveRecord::Base 
    ... 
    attr_accessible :address 
    ... 
end 

現在,我的表單中,我已經知道了source_item_id。我希望能夠在表單中輸入地址,並創建一個target_item和相關的關係。

<%= form_for @new_relationship do |f| %> 

    <% @new_relationship.source_item_id = @current_item.id %> 
    <%= f.hidden_field :source_item_id %>  

    <%= f.submit "New Relationship" %> 
<% end %> 

回答

0

您通常會在控制器中執行關係並讓表單僅收集數據。如果你問如何使用兩種模式的表格,看看這個帖子here。我希望我能理解你的問題!