Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Need help with simple count


  • Please log in to reply
6 replies to this topic

#1 raphaelt

raphaelt

    Passenger

  • Members
  • 7 posts

Posted 27 May 2014 - 12:50 PM

Hello,

 

I'm new to rails and I already researched about this but couldn't find anything helpful

 

I have a table called members and one of the members columns is a boolean type

 

I would like to display in my view, the total number of the members with "true" in that column, like:

<%= @members.count %>

Like, I have 10 members, but only 5 set true in that columns. I want to get this total (5). How can I do this?

 

I made a named_scope, but only works in rails console with:

Member.batizado.count
=> 3

/models/member.rb

scope :batizado, -> { where(baptized: true) }


#2 Ohm

Ohm

    Driver

  • Moderators
  • 469 posts
  • LocationCopenhagen

Posted 27 May 2014 - 08:36 PM

You should make sure that you are setting @members correct in your controller as well.

 

Are you in fact doing something like this:

def index
  @members = Member.batizado
end

Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk


#3 palytoxin

palytoxin

    Signalman

  • Members
  • 11 posts

Posted 28 May 2014 - 02:18 AM

you should use `default_scope` in model

 

http://api.rubyonrai...i-default_scope



#4 Ohm

Ohm

    Driver

  • Moderators
  • 469 posts
  • LocationCopenhagen

Posted 28 May 2014 - 06:22 AM

You should never use a default_scope, unless you are absolutely certain that you want to limit your model with this scope.

 

I normally only use default_scope with inheritance, that is say I have a User class and two other classes RegularUser and Administrator. I would then have a type field on the User class, which I would use default_scope on in each of the others.

class User < ActiveRecord::Base
end

class RegularUser < User
  default_scope -> { where(type: 'RegularUser') }
end

class Administrator < User
  default_scope -> { where(type: 'Administrator') }
end

Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk


#5 raphaelt

raphaelt

    Passenger

  • Members
  • 7 posts

Posted 28 May 2014 - 11:17 AM


 

You should make sure that you are setting @members correct in your controller as well.

 

Are you in fact doing something like this:

def index
@members = Member.batizado
end

 

Ohm, with your method, my list only shows the active records with the "true", but I want to show them all and just get the numbers of true.

 

 

I found a solution:

<%= @members.select {|r| r.baptized}.size %>

But this only gets the "trues". And how do I do if a want to get the "falses"?



#6 Ohm

Ohm

    Driver

  • Moderators
  • 469 posts
  • LocationCopenhagen

Posted 28 May 2014 - 11:28 AM

Yes, it's true that if you use the @members variable to something else, you'll of course get a wrong list here.

 

Instead of your solution, you can do

<%= @members.where(baptized: true).count %>

<%= @members.where(baptized: false).count %>

This of course rely on @members being an ActiveRecord::Relation and not an array. This means that you can't do 

@members = Member.all

in your controller, but should do

@members = Member.scoped

instead.


Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk


#7 raphaelt

raphaelt

    Passenger

  • Members
  • 7 posts

Posted 28 May 2014 - 08:56 PM

Yes, it's true that if you use the @members variable to something else, you'll of course get a wrong list here.

 

Instead of your solution, you can do

<%= @members.where(baptized: true).count %>

<%= @members.where(baptized: false).count %>

This of course rely on @members being an ActiveRecord::Relation and not an array. This means that you can't do 

@members = Member.all

in your controller, but should do

@members = Member.scoped

instead.

 

Didn't work out...

 

undefined method where






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users