Loops RubyonRails 4-在.each中对记录进行排序并创建记录
在我的应用程序中,我有两种型号Loops RubyonRails 4-在.each中对记录进行排序并创建记录,loops,ruby-on-rails-4,each,Loops,Ruby On Rails 4,Each,在我的应用程序中,我有两种型号Post&postText 每个帖子都有语言,位置,标题,说明 职位包括:标题和描述 简短介绍我的申请,以便更好地理解。用户进入并选择他们想要的语言&文本,并将得到他们可以使用的不同帖子文本的结果 例如,用户选择: 标题1=>位置:标题 标题2=>位置:标题 说明1=>位置:说明 语言:英语、西班牙语 目前,我正在通过以下方式创建文本: @languages = params[:languages].split(',') @post_texts = params
Post
&postText
每个帖子
都有语言
,位置
,标题
,说明
职位包括:标题和描述
简短介绍我的申请,以便更好地理解。用户进入并选择他们想要的语言
&文本,并将得到他们可以使用的不同帖子文本的结果
例如,用户选择:
- 标题1=>位置:标题
- 标题2=>位置:标题
- 说明1=>位置:说明
@languages = params[:languages].split(',')
@post_texts = params[:post_texts].split(',')
@post_texts.split(',').each do |id|
@languages.split(',').each do |language|
post_text = PostText.where(post_text_id: id)
@titles = post_text.where(placement: 'title')
@descriptions = banner_text.where(placement: 'description')
end
end
@titles.each do |title|
@descriptions.each do |description|
Post.create(
title: title.body,
description: description.body,
language: 'language here',
)
end
end
如果我只选择1种语言,结果如下:
Title 1 + Description 1
Title 2 + Description 1
English Title 1 + English Description 1
English Title 2 + English Description 1
Spanish Title 1 + Spanish Description 1
Spanish Title 2 + Spanish Description 1
但当我选择几种语言时,文本会混淆,西班牙语文本会混淆英语文本,反之亦然
我想实现的是,当选择了几种语言时,只有英语文本与英语搭配,西班牙语文本与西班牙语搭配等等
因此,结果如下:
Title 1 + Description 1
Title 2 + Description 1
English Title 1 + English Description 1
English Title 2 + English Description 1
Spanish Title 1 + Spanish Description 1
Spanish Title 2 + Spanish Description 1
目前的结果如下:
English Title 1 + English Description 1
English Title 1 + Spanish Description 1
Spanish Title 2 + Spanish Description 1
Spanish Title 2 + English Description 1
@languages = params[:languages].split(',')
@post_texts = params[:post_texts].split(',')
@post_texts.split(',').each do |id|
@languages.split(',').each do |lang|
post_text = PostText.where(post_text_id: id).where(language: lang)
@titles = post_text.where(placement: 'title')
@descriptions = banner_text.where(placement: 'description')
end
end
@titles.each do |title|
@descriptions.where(language: title.language).each do |description|
Post.create(
title: title.body,
description: description.body,
language: 'language here',
)
end
end
ps:如果描述不太好,很抱歉
我也用两种不同的方法进行了测试,但结果相同:
1.
任何帮助都将受到感激并提前感谢 为了获得所需的结果,您需要在上次的
循环中指定它,在那里您可以通过执行.where(field:loop.value)
来创建记录,在您的情况下,它将是where(language:title.language)
,并且与post\u text
变量相同
尚格
致:
代码如下:
English Title 1 + English Description 1
English Title 1 + Spanish Description 1
Spanish Title 2 + Spanish Description 1
Spanish Title 2 + English Description 1
@languages = params[:languages].split(',')
@post_texts = params[:post_texts].split(',')
@post_texts.split(',').each do |id|
@languages.split(',').each do |lang|
post_text = PostText.where(post_text_id: id).where(language: lang)
@titles = post_text.where(placement: 'title')
@descriptions = banner_text.where(placement: 'description')
end
end
@titles.each do |title|
@descriptions.where(language: title.language).each do |description|
Post.create(
title: title.body,
description: description.body,
language: 'language here',
)
end
end
已更改的主要部分是@descriptions.each do | description |
,将其更改为@descriptions.each do | description |