0
選擇數據我有以下結構:從兩個表,並列出
class User < ActiveRecord::Base
has_many :Hobbies, :dependent => :destroy
accepts_nested_attributes_for :hobbies, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
class Hobby < ActiveRecord::Base
belongs_to :User
end
在我Users_controller.rb
def index
@data = User.all(:joins => :hobbies)
end
在index.html.erb
<% for item in @data %>
<tr>
<td><%= item.id %></td> #from table Users
<td><%= item.hobby_name %></td> #from table Hobbies
</tr>
<% end %>
這給我一個錯誤未定義的方法`hobby_name'爲#用戶:0x103cf7480>
我以爲我有那種關聯是正確的,
謝謝你,Kelend! – user1946705 2011-06-09 21:42:27