首页 \ 问答 \ 在Rails中HABTM和连接表的问题(Problem with HABTM and join table in Rails)

在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
更新时间:2022-03-06 11:03

最满意答案

你没有收到编译错误吗?

为什么要在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

相关问答

更多
  • 进入你的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是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,顾名思义,并不意味着像文本字段一样。 这里最大的问题是触发键盘并将任何输入传输到标签节点。 这是一个解决方法。 您可以维护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.
  • 它只是可用的不同场景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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)