0
我有我的應用程序中的用戶模型和列表模型。在同一個對象的異常調試輸出中的Rails差異
pages_controller.rb
class PagesController < ApplicationController
def home
if user_signed_in?
@lists = current_user.lists
# raise @lists.inspect
@new_list = current_user.lists.build
end
end
end
頁/ home.html.erb
<%= raise @lists.inspect %>
現在,我目前的用戶沒有與他相關聯的列表。 當我取消註釋「的頁面#家」
raise @lists.inspect3號線我得到的輸出像這樣:
[]
但是,當我評論說,線路輸出,那裏面home.html.erb異常升高,其輸出如下所示:[#<List id: nil, name: nil, description: nil, user_id: 1, created_at: nil, updated_at: nil>]
爲什麼在同一條@lists.inspect
行中輸出差異?
編輯:當我用@lists = current_user.lists.all
而不是@lists = current_user.lists
然後我在這兩個地方得到一個空的數組。爲什麼兩個代碼之間的行爲差異?
嗯..就是這樣。當我刪除這個'@new_list = current_user.lists.build'時,它顯示相同的輸出。但是空數組已經被分配給@lists,那麼爲什麼下一行會有所作爲呢?我很困惑 – lnreddy
而且,當我使用'@lists = current_user.lists.all'時,那麼這兩種情況下的輸出都是'[]'。那麼,爲什麼差異呢? – lnreddy
@Inreddy因爲在沒有成員的關聯和只有一個默認成員的關聯之間存在差異,這是在不使用參數構建時創建的。 –