2012-09-27 21 views
1

在我previous issue我結束了,下面的代碼似乎按預期方式工作:如何在Squeel語句中使用動態屬性/列?

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 

回答

2

您可以使用send

column_name = :article_id 
commenters.where{ 
    table_name.send(column_name).eq(my{self.id}) 
} 
+0

如何設置_column_name_的值? – Backo

+0

我不知道我明白你在問什麼。我編輯了我的答案,但column_name可以是符號或字符串。只需設置你想檢查的地方的列,就是這樣。 – oldergod

+0

...只需用表名替換table_name即可。 –

相關問題