Rails to symbol problems? Error: undefined method `[]' for nil:NilClass -
background info: deal has many coupons (@freedeals contains coupons) , coupon belongs_to deal.
controller:
@freecoupons = coupon.where(discount: 100).order("created_at desc").page(params[:page]) @deals = deal.all
this code below works want , finds title deal (hard coded in 2 testing purposes)
<% @freecoupons.each |f| %> <%= @deals[2][:title] %> <% end %>
but when switch on trying find title of deal based on coupons association through f.deal_id
<% @freecoupons.each |f| %> <%= @deals[f.deal_id][:title] %> <% end %>
it gives me error "undefined method `[]' nil:nilclass". not sure i'm missing here. symbol problem i'm missing? appreciated.
in coupon model do
belongs_to :deal
then in controller, include deals using includes
- ensure don't make n+1 query:
@free_coupons = coupon.includes(:deal).where(discount: 100).order("created_at desc").page(params[:page])
now in view, can call deal
method on coupon
instance:
@free_coupons.each |f| <%= f.deal.title %> end
Comments
Post a Comment