1

以前の号では、次のコードが期待どおりに機能しているように見えることになりました。

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

変数article_idに対して行われたのと同じくらい動的なステートメントを作成することは可能ですか?table_name

つまり、次のようなものを作りたいと思います。

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
4

1 に答える 1

2

を使用できますsend

column_name = :article_id
commenters.where{
  table_name.send(column_name).eq(my{self.id})
}
于 2012-09-27T01:43:07.120 に答える