2

Here's what I'd really like to do:

Outage.joins{eval "unit.plant"}

I'm going to have a string that represents a squeel keypath. I'd like to send this string into a join statement.

But here's what I get as an error:

#<Class:0x639e328>: unknown class: Squeel::Nodes::Function

I've tried:

Outage.joins{"unit.plant"}

But that does not work... what am I missing?

I don't see any documentation on this on the github page: https://github.com/ernie/squeel/

Thank you for any help!


Update: I've found a really confusing way to pull this off:

("unit.plant".split ".").map{ |x| x.to_sym }.reverse.inject{ |acc, x| { x => acc } }

This will output:

{:unit=>:plant}

This can be passed into the joins squeel function. I'm sure there has to be a better way than to construct nested symbols :(

barancw
  • 888
  • 9
  • 18

2 Answers2

2

How about

Outage.joins{Squeel::Nodes::KeyPath.new("unit.plant".split("."))}

Source: KeyPath Docs

deefour
  • 34,974
  • 7
  • 97
  • 90
  • Thanks! Works perfectly... I looked that over in the docs a few times but I didn't connect that I could just pass it into the constructor like that. I'm still a bit of a Ruby n00b in some areas, but PHP will do that to you! – barancw Jul 25 '12 at 05:07
  • There's a drawback, it cannot be chained with outer/inner like `Outage.joins{Squeel::Nodes::KeyPath.new("unit.plant".split(".")).inner}` – raymondralibi Aug 07 '12 at 17:16
1

Alternative solution, it can be chained with outer/inner:

strs = "unit.plant"
Outage.joins{strs.split(".").inject((strs.present? ? self : nil), :__send__)}

# chain with inner
# Outage.joins{strs.split(".").inject((strs.present? ? self : nil), :__send__).inner}
raymondralibi
  • 1,933
  • 1
  • 17
  • 25