Apurva Mayank
Apurva Mayank

Reputation: 747

How to pass an argument in an method which in being represented as symbol?

I have an action as :

def get_data
  @people = Person.all

  respond_to do |format|
    format.json do
      render :json => {
        :success => true, 
        :people => @people.as_json({
          :only => [:person_name, :text_description, :text_heading],
          :methods => [:title,:age_group],
        })
      }
    end
  end
end

Here title and age_group are my methods in model Person

def age_group
  self.name
end

Now i want to method to look like this

def age_group(age)
  # ...      
end

How do i pass this argument from the controller as the methods representation there is as symbol.

Upvotes: 0

Views: 73

Answers (1)

Amar
Amar

Reputation: 6942

Hi as per my suggestion you can override method or create a instance method depending upon options it will generate hash or json.If you want to use as_json then you can dig into code this line is helpful for digging code https://github.com/rails/rails/blob/2-3-stable/activerecord/lib/active_record/serialization.rb#L33 which will give you how methods being passed.

Upvotes: 1

Related Questions