Conditional check in rabl

978 Views Asked by At

I have question model with has many relationship of question_options.

class Question < ActiveRecord::Base

  #relationship
  belongs_to :user

  has_many :question_options, :dependent => :destroy,:conditions => "is_deactivated is FALSE"

Question option

class QuestionOption < ActiveRecord::Base
  attr_accessible :question_id,:option,:order,:is_other,:is_deactivated
  belongs_to :question

In my question_detail rabl I have

object @question
attributes :id, :status
child :question_options do
 attributes :question_id,:option,:order,:is_other
end

Here I want to respond only the question_option which has is_other = false

like the below....
    object @question
    attributes :id, :status
    child :question_options do
     attributes :question_id,:option,:order,:is_other = true
    end

How do I check a condition in rable that?

1

There are 1 best solutions below

0
On

One easy way to do this is to use if if option on attributes, for example:

object @question
attributes :id, :status
child :question_options do
 attributes :question_id,:option,:order,:is_other, 
    :if => lambda { |question_option| question_option.is_other }
end

Also check out the section on conditions.