So im upgrading rails, and the relations functions now have scope as one of the parameters, and the rest come in as **options. So normally this is fine and you can do something like:
belongs_to :User, class_name: "CompanyUser"
Or something like that. But for the life of me i don't understand how its supposed to work when you call it directly (for example when making an abstract class)
Im updating some legacy code for an abstract method adds some additional Active record relations. To start off here's the signature for belongs_to:
belongs_to(name, scope = nil, **options)
Now the old way was implemented like this:
self.belongs_to relation_name, options
but that makes rails think that options is "scope", so I'm trying to update it to:
self.belongs_to relation_name, nil, options
or
self.belongs_to relation_name, scope: nil, options
or
self.belongs_to(relation_name, nil, options)
Either it is just syntactically bad or i get
/usr/local/bundle/gems/activerecord-7.0.3/lib/active_record/associations.rb:1790:in `belongs_to': wrong number of arguments (given 3, expected 1..2) (ArgumentError)
I understand that the **options is looking for a hash, but options is a hash, idk what its looking for?
CodePudding user response:
You need to specify the **
on your call too. Both of the following could work.
self.belongs_to relation_name, nil, **options
self.belongs_to relation_name, **options
Keep in mind though that self.belongs_to relation_name, scope: nil, **options
would merge scope: nil
to your options
hash not set your scope variable to nil.