我想知道在ruby on rails 4.0中實現我的情況的正確方法。Ruby on Rails:將另外一個模型的兩個引用添加到另一個模型
可以說我有2個模特名爲衆議院和訂購。
我的訂單表應該有兩列從和到這兩個參考房子模型。
在這種情況下,這兩個模型之間應該有什麼關係? 注:我不需要從房子模型的任何參考訂單模型。
我想有這樣的事情在我的Order表
t.references :house, as:from (this should create a column named from and should be of type integer, index of house table
t.references :house, as:to (this should create a column named to and should be of type integer, index of house table
,因爲我想利用房屋的領域在我的訂單像
我想在命令模式這種類型的關係<%= form_for @order do |f| %>
... # order fields
<%= f.fields_for :house(from) do |i| %>
... # your house forms
<% end %>
<%= f.fields_for :house(to) do |i| %>
... # your house forms
<% end %>
...
<% end %>
是否有任何具體的方式來這在軌道?
P.S:我已經在這裏看過這篇文章,但我認爲這並不能完全解決我的問題。 Adding a Model Reference to existing Rails model
非常感謝。這正是我一直在尋找的。我認爲它** **:from_house,:to_house,在訂單模型中爲accept_nested_attributes_for **。我已經做了這個改變。之後,我試圖實現上述,但形式並沒有出現。我的服務器不顯示任何錯誤。所有領域的領域都沒有顯示。我不知道原因是什麼? – Kranthi 2014-09-02 09:21:19
我的不好,有一個錯字!抱歉。你需要在你的控制器中說'@ order.build_from_house'和'@ order.build_to_house',它會出現!如果答案有幫助,那麼請接受答案,因爲答案也可以幫助其他人。 :) – Surya 2014-09-02 09:24:29
它的工作。再次感謝。我已經接受了答案。 – Kranthi 2014-09-02 10:16:09