0

what i am trying to achieve is relatively simple. I have different scopes for each page type in a page model. So in my controller I do this,

  def employer
    @body_class = "employer membership"
    is_page_present?(:employer_pages) #this checks to see if a page has user generated content for the controller action
  end 

private 

  def set_panel_sections(page)
    @page_data = CorporatePage.page.last
    @section_one = @page_data.corporate_panels.section_one
    @section_two = @page_data.corporate_panels.section_two
  end

  def is_page_present?(page_type)
    if CorporatePage.("#{page_type}").any?
      raise
      set_panel_sections(page_type)
    else
      @section_one = nil
    end
  end

I have tried different variations, including without concatenation, but all to no avail.

I get the error

undefined method `call' for #<Class:0x007f833b40b8f0>

and without the concatenation i get undefined method page_type

But in my rails console I can do this..

2.1.2 :001 > CorporatePage.employer_page.any?
   (19.2ms)  SELECT COUNT(*) FROM `corporate_pages`  WHERE `corporate_pages`.`static_descriptor` = 'employer'
 => true 
2.1.2 :002 > CorporatePage.employee_page.any?
   (0.5ms)  SELECT COUNT(*) FROM `corporate_pages`  WHERE `corporate_pages`.`static_descriptor` = 'employee'
 => false 

So, for a more insightful answer I want to know, a method call isnt a string or a symbol, what is it?

How do I fix this current issue?

Thanks

user3868832
  • 610
  • 2
  • 9
  • 25

2 Answers2

1

What you want here is send:

  def is_page_present?(page_type)
    if CorporatePage.send(page_type).any?         
      set_panel_sections(page_type)
    else
      @section_one = nil
    end
  end

send will call the method defined at page_type at the class instance.

Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
0

You need to use the send method. You can find a basic explanation of it here: http://ruby-doc.org/core-2.1.2/Object.html#method-i-send.

In you situation, you would change if CorporatePage.("#{page_type}").any? to if CorporatePage.send(page_type).any? and that should get you what you are after.

dhouty
  • 1,969
  • 12
  • 21