1

Is there any way I can support the Rails 4 syntax for defining associations in Rails 3.2?

I currently have the following Rails 4 syntax:

has_many :clients, -> { where("client_type = 'temp'").order("name DESC") }, through: :user

I want my Rails 3.2 app to support that syntax. In essence, it's as if I had written the following in Rails 3.2:

has_many :clients, conditions: "client_type = 'temp'", order: "name DESC", through: :user

Is there a library/gem that enables this support? Any ideas on how to accomplish this?

Artem Kalinchuk
  • 6,502
  • 7
  • 43
  • 57
  • in Rails 3 you should be able to do `has_many :clients, lambda { where(client_type: 'temp').order('name DESC') }`. The syntax `->` (lambda literal) is a feature of Ruby 1.9 IIRC. You could use rubocop and autofix to convert all `->(arg) { some_logic }` to convert to the explicit `lambda { |arg| some_logic }` syntax https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/Lambda – MrYoshiji Sep 06 '19 at 21:06
  • The exception I get now is `wrong number of arguments (given 3, expected 1..2) (ArgumentError)` – Artem Kalinchuk Sep 06 '19 at 21:09
  • For which method call do you get this error? `has_many` or the `lambda` call itself? – MrYoshiji Sep 09 '19 at 14:58
  • The `has_many` since Rails 3.2 expects 1..2 arguments for it but I'm passing in 3. – Artem Kalinchuk Sep 09 '19 at 20:45

0 Answers0