开发者

undefined method `name' for nil:NilClass Rails

开发者 https://www.devze.com 2023-04-03 14:00 出处:网络
In my User Model I have the following method: def has_birthday_boards? year = Date.new(Time.now.year).to_time

In my User Model I have the following method:

  def has_birthday_boards?
    year = Date.new(Time.now.year).to_time
    self.participants.where(:role => "Celebrant", :created_at => year..Time.now)   
  end

In my controller I have the following:

 def celebrant_exists
   @user =  get_current_board.celebrant
   @participant_boards = @user.has_birthday_boards?

In my view I have the following:

<% @participant_boards.each do |pb|%>
      <%= pb.board.manager.name %>
   <% end %>

This produces the following error:

undefined method `name' for nil:NilClass

So I looked to see if I had an object and I do

<% @participant_boards.each do |pb|%>
      <%= pb.board.manager%>
   <% end %>

Produces:

#<User:0x0000010645b800> #<User:0x00000106422708> 

And if 开发者_StackOverflow中文版I do the following:

@participant_boards.first.board.manager.name

=> Benedict Dickens 

So why can't I loop through the array and get these values?


Probably some board does not have manager. Check this:

<% @participant_boards.each_with_index do |pb, i|%>
  <%= i %>
  <%= pb.board.manager%>
<% end %>

Probably you will see some numbers without pb.board.manager

0

精彩评论

暂无评论...
验证码 换一张
取 消