1

I am new to rails. I have three models tickets, tags and comments with relationships and it is working fine.

I want to display the total number of tickets in my ticket index view, but I don't know why... I think that this is a really easy answer for you guys...

<%= ticket.count %> says undefined method.

Can you help me or do you need more informations? Thank you!

2 Answers 2

1

Assuming that in TicketController you have something like this:

  def index
    @tickets = Ticket.all
  end

In your index view, to display the count of tickets, do as follows:

<%= @tickets.count %>
<% @tickets.each do |ticket| %>
   .....

<% end %>

Call the count method on the collection object @ticket(Array of type ActiveRecord::Relation) and not on the ticket which is an instance of Ticket class.

Sign up to request clarification or add additional context in comments.

Comments

1

In controller load ticket count

@ticket_count = Ticket.all.count

in view

<%= @ticket_count %>

ticket.count will not work because ticket is object of Ticket class which does not have count method defined . you can define count method for ticket and compute total of all Ticket then it will surely work.

I suggest to use Ticket.all.count which will return total no of tickets

3 Comments

Beat me to it -- you should mention the difference between instances and classes for why ticket.count didn't work.
@Pyrce thanks for reminding , anyways if i missed anything please fill in.
That also works, but wich way is the best practice way? Your one or the answer below?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.