传输数据到连接表:Rails 4(Transfer data to join table: Rails 4)
我有一个滑轨模型
Game
和另一个模型Player
。 目前,分配给游戏的玩家的数据被存储在games
桌中的(玩家ID)数组中。我试图创建一个连接表来连接游戏,并且我创建了一个
games_players
连接表games_players
,然后我尝试运行迁移以在传输另一个迁移之前传输数据,以从games
表中删除players
数组。我的代码传输数据如下:
games = Game.all games.each do |g| game = g games.players.each do |p| game.players << p end end
但它并不在联接表中保存数据,而是在
games
表中删除数组列。 在删除属性之前,如何将所有这些数据传输到新的连接表中?补充说明:
class GamesPlayers < ActiveRecord::Base belongs_to :game, inverse_of: :games_players belongs_to :player, inverse_of: :games_players end class Game < ActiveRecord::Base has_many :games_players, inverse_of: :game has_many :players, through: :game_players end class Player < ActiveRecord::Base has_many :games_players, inverse_of: :player has_many :games, through: :games_players end
I have a rails model
Game
and another modelPlayer
. Currently, the data for players assigned to a game was being stored in an array (of player ids) in thegames
table.I'm trying to create a join table to connect games and I created a
HABTM
join tablegames_players
and then I'm trying to run a migration to do transfer the data before running another migration to delete theplayers
array from thegames
table.My code to transfer the data is as follows:
games = Game.all games.each do |g| game = g games.players.each do |p| game.players << p end end
But it doesn't save the data at all in the join table rather it goes ahead with deleting the array column in the
games
table. How can I transfer all this data to the new join table before deleting the attribute?Added associations for clarification:
class GamesPlayers < ActiveRecord::Base belongs_to :game, inverse_of: :games_players belongs_to :player, inverse_of: :games_players end class Game < ActiveRecord::Base has_many :games_players, inverse_of: :game has_many :players, through: :game_players end class Player < ActiveRecord::Base has_many :games_players, inverse_of: :player has_many :games, through: :games_players end
原文:https://stackoverflow.com/questions/49287286
最满意答案
尝试使用此导入
import org.gradle.internal.logging.*
import org.gradle.internal.logging.* task intercept { def outputStream = new File("gradle.log") gradle.services.get(LoggingOutputInternal).addStandardOutputListener (new StandardOutputListener () { void onOutput(CharSequence output) { outputStream << output } } ) }
Try with this import
import org.gradle.internal.logging.*
import org.gradle.internal.logging.* task intercept { def outputStream = new File("gradle.log") gradle.services.get(LoggingOutputInternal).addStandardOutputListener (new StandardOutputListener () { void onOutput(CharSequence output) { outputStream << output } } ) }
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
如果你这样开始,你有两种可能: 您可能希望将compile配置设置为从provided扩展。 仅供参考, 这里和这里有一些有趣的讨论。 configurations { provided compile.extendsFrom provided } 另一种方法是应用eclipse插件并使用此处所述的plusConfiguration属性将provided配置添加到Eclipse类路径。 eclipse { classpath { plusConfigurations += [ ...
-
gradle中的自定义任务/插件(Custom Task/Plugin in gradle)[2023-10-27]
这不是它的工作原理。 如果您需要提供在包中组织的自定义插件和任务,请将整个代码放在buildSrc目录下。 在$GRADLE_HOME/samples/multiProjectBuildSrc你可以找到如何完成的优秀示例。 $GRADLE_HOME - gradle安装目录。 This is not how it works. If you need to provide custom plugins and tasks organized in packages , put the whole code ... -
尝试使用此导入import org.gradle.internal.logging.* import org.gradle.internal.logging.* task intercept { def outputStream = new File("gradle.log") gradle.services.get(LoggingOutputInternal).addStandardOutputListener (new StandardOutputListener () { ...
-
像War这样的任务类型不是为用户设计的。 通常更好(也更容易)编写插件(在最简单的情况下,只是apply from: "path/to/script.gradle"另一个构建脚本),而不是子类化和使用大量内部构件,它们使用预先配置任务他们的公共API。 例如: tasks.withType(War) { eachFile { FileCopyDetails details -> ... } } 如果绝对必要,这样的配置也可以进入子类的构造函数。 也就是说,我不确定你为什么要用Ant类来解决类加载器 ...
-
jar的Gradle自定义任务(Gradle custom task for jar)[2022-02-02]
build任务来自java插件。 您可以使用以下内容控制jar的构建时间: jar { onlyIf { condition } } 您可以将jar设置为在声明其他内容为true时生成,或者将其设置为false以从不构建jar。 您可以在toJar自定义任务中包含toJar ,以将已编译的文件包含到jar中。 task toJar (type: Jar) { from sourceSets.all } 您明确要求所有编译时依赖项都包含在jar中: from configurations.co ... -
是。 build.gradle导入与纯java类中的导入完全相同。 import foo.bar.TestNGBlock task blockC(type: TestNGBlock) { testngxml = "tests/testng-lrga-blockC.xml" } Yes. Import in build.gradle works exactly the same as in pure java class. import foo.bar.TestNGBlock task blockC ...
-
下面的代码可以解决这个问题(应该放在myapp/build.gradle ): allprojects.findAll { it.name in ['myapp-client', 'myapp-shared'] }. each { p -> configure(p) { task herpderp(type: HerpDerpTask) } } class HerpDerpTask extends DefaultTask { @TaskAction def herp ...
-
Gradle是新Android构建系统的主要组件,随新的Android开发IDE,AndroidStudio一起推出。 Gradle用户指南是一本很好的读物。 可以在Eclipse中使用新的基于gradle的构建系统,只需按照此处指出的步骤操作: 在Android项目中是否有任何方法可以将Eclipse与Gradle集成? Gradle is the main component of the new Android build system that was introduced with the new ...