-2

I tested, that they, in fact, don't work:

class User < ActiveRecord::Base

  def self.call_protected_method
    protected_method
  end

  def self.call_private_method
    private_method
  end

  protected

  def self.protected_method
    puts "protected_method"
  end

  private

  def self.private_method
    puts "private_method"
  end

end

What i mean, by they don't work, is You can call all methods in this example. It doesn't matter if they are under private and/or protected.

# in rails console:
User.call_protected_method # => protected_method
User.protected_method      # => protected_method
User.call_private_method   # => private_method
User.private_method        # => private_method

Why is that? Whats the reason for ignoring 'private' and 'protected'?

UPDATE: my question isn't how to do this. My question is why doesn't this approach work in rails models!?

ExClouds
  • 527
  • 1
  • 7
  • 13
  • 2
    What did you expect to happen, what happened instead? – Nils Landt Apr 20 '16 at 13:57
  • What i mean, by they don't work, is You can call all methods in this example. It doesn't matter if they are under private and/or protected. Why is that? Whats the reason for this? – ExClouds Apr 20 '16 at 14:07

1 Answers1

1

You are trying to define a private class method, and that would not work with that syntax. You want private_class_method

Have a look at these answers, too: Ruby class with static method calling a private method?

Community
  • 1
  • 1
eugen
  • 8,916
  • 11
  • 57
  • 65
  • my question isn't how to do this. My question is why doesn't this approach work in rails models!? – ExClouds Apr 20 '16 at 14:17
  • 1
    Rails has nothing to do with it, it's a ruby language feature. This article might explain the reasons why this behaviour exists: http://jakeyesbeck.com/2016/01/24/ruby-private-class-methods/ – eugen Apr 20 '16 at 14:22