albertojauregui
albertojauregui

Reputation: 3

Filter with where inside render json

I have this in a controller:

def get_tags
  person = Person.where({fuid: params[:fuid]})
  render json: person, :only=>[:fuid], :include => [
    :hashtags => {
      :except => [:created_at, :updated_at],
      :methods => :person_count
    }
  ]
end

I am including hashtags but I need to filter those hashtags for only the ones that have active = 1. Is there a way to do this?

Upvotes: 0

Views: 41

Answers (1)

SickLickWill
SickLickWill

Reputation: 196

You could do this in the model:

class Person < ActiveRecord::Base
  has_many :active_hashtags,-> { where active: 1 }, class_name: 'Hashtags' 
end

Then in your controller:

render json: person :only=>[:fuid], :include => [
    :active_hashtags => {
      :except => [:created_at, :updated_at],
      :methods => :person_count
    }
  ]

Upvotes: 1

Related Questions