Rails Learner驱动程序Q。
DB Entities -2\f25 Master -2\f25 Detail relationships 2\f6(数据库实体-2\f25 Master-2\f6详细关系
每家城市has_many酒店,每家酒店has_many客房
每间客房belongs_to酒店每家酒店belongs_to City
我在酒店房间里醒来--但是我怎么知道我在哪个城市呢?
@Room.hotel.name =酒店名称
但是,我可以使用什么语法来指代“我在哪个城市?”
为了遍历2个主详细关系?
在Java语言中应该很简单: string cityName = room.gethotel().getcity().getname()
在Rails中-这是使用:通过=> :hotel实现的吗?
或者,这仅用于定义多对多关系。
对我来说,最好的答案是指出一本书中贯穿这一切的一章,好吗?
非常感谢!
发布于 2014-04-02 07:38:11
=========================================================
http://guides.rubyonrails.org/association_basics.html#the-has-one-through-association
=========================================================
has_one :city, through: :hotel
<%= @room.city.name %>=========================================================
city.rb类城市<活动记录::基础has_many :酒店结束
hotel.rb类酒店< ActiveRecord::Base has_one :city has_many :rooms
room.rb类酒店< ActiveRecord::Base has_one :酒店has_one :城市,通过::酒店结束
景观/房间
show.html.erb <%= @room.city.name %>
index.html.erb <%= room.city.name %>
===================================================================
https://stackoverflow.com/questions/22767511
复制相似问题