在Rails中HABTM和连接表的问题(Problem with HABTM and join table in Rails)
我有一个简单的模型:
class User < ActiveRecord::Base has_and_belongs_to_many :roles end class Role < ActiveRecord::Base has_and_belongs_to_many :users end
我创建了一个简单的连接表:
class CreateUsersRoles < ActiveRecord::Migration def self.up create_table :users_roles, :id => false do |t| t.integer :user_id t.integer :role_id t.timestamps end end def self.down drop_table :users_roles end end
迁移后,shema.rb正在关注:
create_table "roles", :force => true do |t| t.string "name" t.datetime "created_at" t.datetime "updated_at" end create_table "users", :force => true do |t| t.string "login" t.string "password" t.datetime "created_at" t.datetime "updated_at" end create_table "users_roles", :id => false, :force => true do |t| t.integer "user_id" t.integer "role_id" t.datetime "created_at" t.datetime "updated_at" end
这些条目以相同的顺序显示在“schema.rb”中
我按照以下方式安排了固定装置:
# roles.yml user: name: user admin: name: admin moderator: name: moderator # users.yml User1: login: User1 password: User1 roles: user User2: login: User2 password: User2 roles: admin, moderator User3: login: User3 password: User3 roles: moderator
并遇到一个问题:在“rake db:fixtures:load”rails上抱怨连接表的名称:
SQLite3 :: SQLException:没有这样的表:roles_users:DELETE FROM“roles_users”WHERE 1 = 1
问题是 - 为什么它期望“roles_users”,而表是“users_roles”?
I have a simple model:
class User < ActiveRecord::Base has_and_belongs_to_many :roles end class Role < ActiveRecord::Base has_and_belongs_to_many :users end
I have created a simple join table:
class CreateUsersRoles < ActiveRecord::Migration def self.up create_table :users_roles, :id => false do |t| t.integer :user_id t.integer :role_id t.timestamps end end def self.down drop_table :users_roles end end
After migration, shema.rb is following:
create_table "roles", :force => true do |t| t.string "name" t.datetime "created_at" t.datetime "updated_at" end create_table "users", :force => true do |t| t.string "login" t.string "password" t.datetime "created_at" t.datetime "updated_at" end create_table "users_roles", :id => false, :force => true do |t| t.integer "user_id" t.integer "role_id" t.datetime "created_at" t.datetime "updated_at" end
The entries are presented here in the same order, as in "schema.rb"
I arranged fixtures in following way:
# roles.yml user: name: user admin: name: admin moderator: name: moderator # users.yml User1: login: User1 password: User1 roles: user User2: login: User2 password: User2 roles: admin, moderator User3: login: User3 password: User3 roles: moderator
And got a problem: on "rake db:fixtures:load" rails complains about name of a join table:
SQLite3::SQLException: no such table: roles_users: DELETE FROM "roles_users" WHERE 1=1
The question is - why it expects "roles_users", while the table is "users_roles" ?
原文:https://stackoverflow.com/questions/3288330
最满意答案
你没有收到编译错误吗?
为什么要在didMoveToView中创建方法?
您的代码看起来应该更像这样
class GameScene: SKScene { var testShape = SKLabelNode() override func didMove(to view: SKView) { let delay = SKAction.wait(forDuration: 2) let repeatingAction = SKAction.run(repeater) let sequence = SKAction.sequence([ delay, repeatingAction ] ) run(SKAction.repeatForever(sequence)) } func createShape() { testShape = SKLabelNode(text: "TEST") testShape.position = CGPoint(x: 0.5, y: 0.5) testShape.zPosition = 1 addChild(testShape) } func repeater() { createShape() let moveTestShape = SKAction.moveBy(x: -500, y: 0, duration: 5) testShape.run(moveTestShape) } }
这就是你在SKActions中调用函数/代码块的方法。
let repeatingAction = SKAction.run(repeater)
要么
let repeatingAction = SKAction.run { repeater() }
还记得我们只重复新标签的spawn动作。 移动标签的实际操作不重复。 所以你应该看到的是1个标签创建并移动一次,而不是2秒后创建一个新标签并移动一次等
希望这可以帮助
Are you not getting compiler errors?
Why are you creating methods in didMoveToView?
Your code should look more like this
class GameScene: SKScene { var testShape = SKLabelNode() override func didMove(to view: SKView) { let delay = SKAction.wait(forDuration: 2) let repeatingAction = SKAction.run(repeater) let sequence = SKAction.sequence([ delay, repeatingAction ] ) run(SKAction.repeatForever(sequence)) } func createShape() { testShape = SKLabelNode(text: "TEST") testShape.position = CGPoint(x: 0.5, y: 0.5) testShape.zPosition = 1 addChild(testShape) } func repeater() { createShape() let moveTestShape = SKAction.moveBy(x: -500, y: 0, duration: 5) testShape.run(moveTestShape) } }
This is how you call functions/code blocks in SKActions.
let repeatingAction = SKAction.run(repeater)
or
let repeatingAction = SKAction.run { repeater() }
Also remember our are only repeating the spawn action for new labels. The actual action to move the labels is non repeating. So what you should see is 1 label created and moved once, than 2 seconds later a new label gets created and moved once etc
Hope this helps
相关问答
更多-
SpriteKit设备方向改变导致SKLabelNode失真Swift(SpriteKit device orientation change causes SKLabelNode distortion Swift)[2023-01-22]
进入你的Storyboard文件,选择你的ViewController的视图,在右边的栏上找到滑块图片,它应该是左边的第4个。 这称为属性检查器。 将内容模式更改为中心。 这会给你黑条但停止压扁。 Go into your Storyboard file, Select your ViewController's view and on the right hand bar, look for the slider image, it should be the 4th one from the left. ... -
延迟基于字体的加载。 最好预加载您打算使用的字体,声音和任何其他资源,以便在第一次实际使用时没有延迟。 您可以在您的设置中预加载: SKLabelNode *preload = [SKLabelNode labelNodeWithFontNamed:@"System"]; [preload setText:@"anything"]; 正如评论中所述,只有在使用iOS不支持的字体时才需要预加载。 The delay is based on the loading of your font. Best to ...
-
SKLabelNode还是UILabel?(SKLabelNode or UILabel?)[2023-09-23]
SKLabelNode是Sprite Kit框架的一部分,只能添加到SKScene。 UILabel是UIKit的一部分,可以添加到UIView中。 如果您正在使用通用应用程序,则需要使用UIKit和UILabel; Sprite Kit通常用于游戏。 SKLabelNode is part of the Sprite Kit framework, and can only be added to a SKScene. UILabel is part of UIKit, and can be added t ... -
默认情况下,你的主场景的锚点被设置为x = 0.5和y = 0.5,所以,当你添加一个新节点时,它的坐标从屏幕中心开始。 现在,您可以将场景的定位点更改为 self.anchorPoint = CGPoint(x: 0.0, y: 0.0) let label = SKLabelNode(text: "Player Score") label.fontColor = SKColor.white label.fontSize = 60 label.position = ...
-
使SKLabelNode可编辑(Making an SKLabelNode editable)[2022-10-15]
你所说的非常接近于重新发明轮子。 SKLabelNode,顾名思义,并不意味着像文本字段一样。 这里最大的问题是触发键盘并将任何输入传输到标签节点。 这是一个解决方法。 您可以维护UITextField并将其隐藏在SKView上。 它的目的是处理来自键盘的输入,这些输入应反映在SKLabelNode上。 需要在SKScene类中添加以下代码。 它有效,我自己验证了它。 码 维护UITextField的实例变量。 我假设标签节点也可以从类中的任何地方访问。 UITextField *field; SKLabe ... -
你没有收到编译错误吗? 为什么要在didMoveToView中创建方法? 您的代码看起来应该更像这样 class GameScene: SKScene { var testShape = SKLabelNode() override func didMove(to view: SKView) { let delay = SKAction.wait(forDuration: 2) let repeatingAction = S ...
-
将要使用的字体名称放入本地化,然后使用NSLocalizedString获取它。 Put the name of the font to use into your localization, and use NSLocalizedString to fetch it.
-
SKLabelNode fontSize(SKLabelNode fontSize)[2022-10-10]
它只是可用的不同场景scaleModes。 我还没有玩过太多他们。 我的游戏都使用.ResizeFill来确保场景没有被裁剪,但结果我必须相应地调整我的标签/精灵尺寸。 示例项目使用.AspectFill,它确保标签/精灵的大小相同,但结果你的场景可能会被裁剪,这在我的游戏中我不想要。 我刚刚发布了我的助手,用于调整我在GitHub上的组件,感兴趣的是有人感兴趣。 https://github.com/crashoverride777/Device-Manager It just turns out to ... -
不要将fontColor的alpha设置为0, SKLabelNode在对其运行fadeIn操作之前将fadeIn的alpha为0。 这是因为操作应用于节点本身,而不是应用于节点内的属性。 (EG在你的情况下: fadeIn影响SKLabel.alpha ,而不是SKLabel.fontColor.alpha ) Instead of setting the fontColor's alpha to 0, set the SKLabelNode's alpha to 0 before running the ...
-
首先,您必须添加一个字符串文件: 然后打开项目设置并添加新语言: 将添加的字符串文件标记为目标: 找到新添加的本地化文件。 (自动添加英文) First you have to add a Strings File: Then open the project settings and add a new language: Mark your added strings file as target: Find the newly added localising file. (English is au ...