1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Ruby on Rails | Postgres : How to find records with status (which is calculated based on...

Discussão em 'Outras Linguagens' iniciado por Stack, Maio 18, 2021.

  1. Stack

    Stack Membro Participativo

    I have a listing page of schools where I have to list the name and status of the school. The status is calculated based on the student's status.

    Here is my models...

    class School < ActiveRecord::Base
    has_many :students
    has_many :teachers
    end

    class Student < ActiveRecord::Base
    belongs_to :school

    enum status: {
    active: 'active',
    suspended: 'suspended'
    }
    end


    So, the idea is list the school name and school status as

    1. school status is active when at least one student is in active state.
    2. school status is suspended when no active student but student record exists.
    3. school status is closed when there is no students record against school.

    I am trying hard to find the query to get the data like I wanted. Since this is a has_many I am finding it difficult for me. I use postgresql database.

    Any help will be appreciated.

    Note: I need the sql query to find this. (something like combining two tables, calculating the status based on the condition)

    [​IMG]

    Continue reading...

Compartilhe esta Página