我將Couchrest_model用作CouchDB的Rails ORM。我想讓Rails爲來自兩個不同的CouchDB文檔的信息「加入」視圖,但似乎無法讓Rails自動檢索聯合數據。在Rails視圖中加入兩個CouchDB模型的信息
這裏有兩個相關機型:
class Package < CouchRest::Model::Base
belongs_to :vendor
property :shortcode, String
property :pins, Integer
end
class Vendor < CouchRest::Model::Base
property :vendor, String
timestamps!
end
所以,現在在我的index.html.erb對/包我想從我的兩個模型顯示與數據表:
<h1>Listing packages</h1>
<table>
<tr>
<th>Shortcode</th>
<th>Pins</th>
<th>Vendor</th>
<th></th>
</tr>
<% @packages.each do |package| %>
<tr>
<td><%= package.shortcode %></td>
<td><%= package.pins %></td>
<td><%= package.vendor %></td>
<td><%= link_to 'Show', package %></td>
<td><%= link_to 'Edit', edit_package_path(package) %></td>
<td><%= link_to 'Destroy', package, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Package', new_package_path %>
我想要顯示供應商模型中的供應商字符串。我在視圖中使用了一個選擇器幫助器來顯示跨CouchDB模型的「加入」信息,但我無法弄清楚如何加入這個看起來更簡單的情況,只是將視圖表中的字符串打印出來。
下面是對應於指數,這是非常標準的封裝控制器:
class PackagesController < ApplicationController
# GET /packages
# GET /packages.json
def index
@packages = Package.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @packages }
end
end
我試着做標準
@packages = Package.all(:include => :vendor)
但Couchrest_model不會在供應商信息拉那樣...
那麼,我能想到的最直接的解決方案就是這樣做:<%= Vendor.get(package.vendor_id).vendor%>。我不知道這是否是最好的「Rails方式」來做到這一點,但它是有效的。如果任何人有更好的(即更清潔)解決方案,我會非常樂意給他們一張支票! – Sly 2011-12-31 20:08:24