has_many:通过url / view中的关联?

时间:2012-04-16 12:56:49

标签: ruby-on-rails ruby-on-rails-3

我在我的房屋模型中添加了has_many :through关联(联接表),因此房子可以有多个类别,例如“lastminute”,“house with pool”等。

我做了一个自定义查询,所以我希望所有的房子都在“lastminute”类别中(5)

@lastminutes = House.find(:all, :select=>"houses.*", :joins=>"JOIN category_join_tables ON houses.id = category_join_tables.house_id", :conditions=>["category_join_tables.house_id = houses.id AND category_join_tables.category_id = ?", 5])

这在控制台中工作正常。

在我的网站上使用此功能的最佳方法是什么?我想网址结构喜欢这个:

domain.com/locale/holidayhouses/lastminutes (house in the lastminute category)
domain.com/locale/holidayhouses/holidayhouses_with_pool (houses in the pool category)

domain.com/locale/lastminutes (houses and appartments in the lastminute category)

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:1)

这很容易:

配置/ routes.rb中:

match 'search/*categories' => "controller#action"

在您的操作中,您现在可以将类别作为数组获取,只需要将它们解析为查询。我猜您需要“搜索”,因为否则rails会将每个正常请求作为类别并将您重定向到该操作。

请参阅How to embed multiple tags in Rails routes, like Stack Overflow


对于l18n部分你可以像

那样做
scope "/:locale" do
  # move all the other routes in here to work with the locale
end

查看I18N上的rails guide