1

In my previous issue I ended up that the following code seems to work as expected:

def my_squeel_query
  table_name = Squeel::Nodes::Stub.new("#{self.class.to_s.tableize}_comment_associations".to_sym)

  commenters.
    .where{
      table_name.article_id.eq(my{self.id})
    }
end

Is it possible to make the article_id statement as dynamic as made for the table_name variable?

That is, I would like to make something as-like the following:

def my_squeel_query
  table_name = Squeel::Nodes::Stub.new("#{self.class.to_s.tableize}_comment_associations".to_sym)

  commenters.
    .where{
      table_name.<DYNAMIC_KEY>.eq(my{self.id}) # '<DYNAMIC_KEY>' refers to a column name present in the 'commenters' database table.
    }
end
Community
  • 1
  • 1
Backo
  • 18,291
  • 27
  • 103
  • 170

1 Answers1

2

You can use send.

column_name = :article_id
commenters.where{
  table_name.send(column_name).eq(my{self.id})
}
oldergod
  • 15,033
  • 7
  • 62
  • 88
  • How to set the `_column_name_` value? – Backo Sep 27 '12 at 01:44
  • I am not sure I understand what you are asking. I edited my answer but column_name can be a symbol or a string. Just set the columns you want to check for somewhere and that is it. – oldergod Sep 27 '12 at 01:54
  • ...and just replace `table_name` with you table name. – Janko Apr 12 '13 at 19:50
  • 1
    Better yet, you can use `__send__`, to avoid having to type the table name: `commenters.where{__send__(column_name).eq(my{self.id})}` – Janko Jul 09 '13 at 10:17