0
我的應用程序中有2個表格1. Users,2. Restaurants。用戶可以保存他們去過的餐廳的名字(以及其他屬性)。例如用戶1曾去過熊貓快車和紅羅賓斯。這些餐廳記錄還有一個「食品類別」作爲其記錄的屬性。當另一個用戶(用戶2)登錄用戶1的個人資料頁面時,有一列列出用戶1的不同餐館食物類別(例如美國和中國)。查找包含軌道中特定屬性的所有記錄
我想要做的是允許用戶2點擊食品類別來過濾和顯示僅點擊類別下的餐館。 (而不是顯示所有餐館,如果用戶2點擊中文,則只顯示熊貓快遞。)
如何將食品類別參數傳遞給餐館模型以過濾結果?
-
Users table: user_id | name | email
1 | Bob | [email protected]
2 | Alice | [email protected]
Users restaurants table: users_restaurants_id | food_category | user_id
1 | Chinese | 1
2 | American | 1
Restaurants Table: restaurant_id | name | food_category | user_id
1 | Panda Express | Chinese | 1
2 | Red Robins | American | 1
-
Users Show view
<%= for each @restaurants do |r| %>
<%= link_to r.name, url => { :controller => users, :action => show, :xxx => r.id }
<% end %>
Users controller
def show
@user = User.find(params[:id])
whichfoodcategory => params(:xxx)
unless whichfoodcategory.nil?
#just render all restaurants for all food categories
@restaurants = @user.restaurants
else
#use the params(:xxx) to filter the restaurants model records for @user... but how?
@restaurants = @user.filteredbyfoodcategory
end
end
Restaurants Model
attr_accessor :xxx(?) or :whichfoodcategory(?)
named_scope :filteredbyfoodcategory { select all where user_id = 1 and food_category = :whichfoodcategory? or xxx? }
-
我敢肯定,我應該在餐館模型使用named_scope,但我不知道如何開始將食品類別傳遞給模型。