Undefined method for ActiveRecord::Relation

I have made a method called closed? in my HelpDesk model.

I am getting the following error.

undefined method 'closed?' for #<ActiveRecord::Relation:0x19311c0>

My 2 models are

class HelpDesk < ActiveRecord::Base
  belongs_to :user
  
  def closed?
    return false if closed == 0
  end
end
class User < ActiveRecord::Base
  has_many :help_desks
end

Then my controller is

def helpdeskcalls
  user = User.find(current_user.id)
  @help_desks = user.help_desks.closed?
end

I’m not sure what this error means, Any help would be appreciated.

Take a look at your help desk model, you have a question mark in the signature. I don’t believe that’s allowed.

S

I still get the same error without the ? I’m afraid :frowning:

I eventually got it to work, I was calling a method on a relation, I needed to make a class method instead.

So now my model is:

class HelpDesk
  def self.open_calls
    self.where(closed: 0)
  end
end

and the controller is

def helpdeskcalls
  user = User.find(current_user.id)
  @help_desks = user.help_desks.open_calls
end

If there is a better way at doing this please let me know :smile:

Can you elaborate a bit more on the app and what its doing? I can guess, but would rather not presume anything. In this case that open “calls” thing could be a property rather than a method on the model. Is it something that you want to know everytime the model is called, or only sometimes? Could it be something you would search on for a given user?

This is also the use case for Rails “scopes”:

Although, scopes are essentially doing the same thing as calling a class method:

class HelpDesk < ActiveRecord::Base
  scope :open_calls, -> { where(closed: 0) }
end

is the same as:

class HelpDesk < ActiveRecord::Base
  def self.open_calls
    where(closed: 0)
  end
end

@crispincornett I was going to use a scope, but i was told that class methods were better?

@steveportock The app is an online helpdesk and I am using it as a view all open help desks for the current user.

Scott, to me this would be a property, or scope, sorry if my terms are off here, I am coming from the .net c# world to this. I would have this a property of model, like a status, “open” helpdesks or closed ones. If I understand this correctly, it would be something you would query on no?

@steveportock Yes it is something I would query on, quite a lot.

Scott, so what does your your app look like, in terms of all the models and how they interact? Is this working now or still being built? Was any of us here able to help?

@steveportock It’s still a work in progress but the model now looks like this:

class HelpDesk < ActiveRecord::Base
  def self.open_calls
    where(closed: 0)
  end

  def self.closed_calls
    where(closed: 1)
  end
end

Yes you were both able to help so thank you.

In Ruby “?” are allowed in method names. By convention names that end in a question mark are used for predicate methods. These are methods that return a boolean.

@sankage Thank you for clarifying that.

Sankage, thanks, as I get into this, I am taking a step back and spending more time learning the language. I got too fast into building things rather than getting a better understanding of how the language works in general.

S