我希望将两个领域连接在一起,最好在两个领域之间加一个空格。我知道这可以在模型级别完成,但是我无法预测哪些字段和表格会混合在一起,因此我无法为此创建虚拟属性。
以下是我试图做的一个过于简单的例子。
表:
# Product (id: integer, name: string, variety: string)
# Location (id: integer, city: string)
这会给我一个位置关系,以及产品种类:
p=Product.joins(:location).group(:location_id, :variety).pluck(:city, :variety)
现在我需要将位置名称和品种名称一起加入;该位置来自位置city
,variety
来自产品。
这只是一个例子,但我还有更多我无法预测的组合。我更倾向于避免为此创建虚拟属性,因为为每个可能的组合添加属性会非常复杂,并且使用受影响的模型创建自己的函数对于这个问题会有点太多简单。
答案 0 :(得分:0)
为什么你不能简单地在采摘后加入结果?像这样:
public bool WriteFile(byte[] byteContent, DateTime dtTimeStamp, string urlFilename)
{
HttpContext context = HttpContext.Current;
HttpResponse response = context.Response;
response.Clear();
response.ClearHeaders();
response.ClearContent();
response.BufferOutput = true;
response.AppendHeader("Content-Length", byteContent.Length.ToString());
response.AppendHeader("Content-Type", "application/octet-stream");
response.AppendHeader("Last-Modified", dtTimeStamp.ToString("R"));//Last-Modified Wed, 28 Aug 2013 10:16:46 GMT
response.AppendHeader("Content-Disposition", "inline; filename=\"" + urlFilename + "\"");
response.BinaryWrite(byteContent);
response.Flush();
// Prevents any other content from being sent to the browser
response.SuppressContent = true;
context.ApplicationInstance.CompleteRequest();
return true;
}
答案 1 :(得分:0)
我知道这是一个老问题,但它出现在Google搜索中,我找到了更好的答案。
@ philip-hallstrom的答案可行,但它会在内存中执行连接。在数据库级别执行该连接会更有效,如下所示:
Product
.joins(:location)
.group(:location_id, :variety)
.pluck("CONCAT_WS(' ', locations.city, products.variety)")