Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to override the << operator in Ruby?

I'm trying to do something like:

account.users << User.new

But I need users to be a method on an account. So I've tried things like:

def users<<(obj)

But I've had no luck with that. Is this even possible to do in Ruby? I would assume so because the ActiveRecord relationships seem to work this way in Rails.

like image 778
Matthew Stopa Avatar asked Mar 02 '12 16:03

Matthew Stopa


1 Answers

Check this answer: Rails: Overriding ActiveRecord association method

[this code is completely from the other answer, here for future searchers]

has_many :tags, :through => :taggings, :order => :name do
    def << (value)
      "overriden" #your code here
    end     
  end
like image 123
Jesse Wolgamott Avatar answered Nov 02 '22 19:11

Jesse Wolgamott