首页 \ 问答 \ ActiveRecord :: AssociationTypeMismatch:for has_many:through relationship(ActiveRecord::AssociationTypeMismatch: for has_many :through relationship)

ActiveRecord :: AssociationTypeMismatch:for has_many:through relationship(ActiveRecord::AssociationTypeMismatch: for has_many :through relationship)

我正在塑造一个有很多玩家的游戏。 玩家还可以玩多个游戏。 我有用户,游戏和plays_game作为连接表。

这是我第一次涉足has_many:通过关系,所以我希望这是一个简单的问题。 或者至少是一个简单分辨率的问题。

class User < ActiveRecord::Base
   has_many :played_games
   has_many :games, :through => :played_games  
end

class Game < ActiveRecord::Base
   has_many :played_games
   has_many :users, :through => :played_games
end

class PlayedGame < ActiveRecord::Base
  belongs_to :users
  belongs_to :games
end

当我尝试将游戏添加到用户(从控制台)时会发生这种情况:

User.first.played_games << Game.first

结果是:

ActiveRecord::AssociationTypeMismatch: PlayedGame(#70279902258580) expected, got Game(#70279901145600)

好吧,也许我在误解。 也许这是我应该尝试添加的games

User.first.games << Game.first.id

结果是:

NameError: uninitialized constant User::Games

任何帮助或文档链接表示赞赏。 提前致谢!


I am modeling a game which has many players. Players can also play multiple games. I have users, games and played_game which is acting as the join table.

This is my first foray into has_many :through relationships so I'm hopeful that this is a simple question. Or at least a question with a simple resolution.

class User < ActiveRecord::Base
   has_many :played_games
   has_many :games, :through => :played_games  
end

class Game < ActiveRecord::Base
   has_many :played_games
   has_many :users, :through => :played_games
end

class PlayedGame < ActiveRecord::Base
  belongs_to :users
  belongs_to :games
end

This is what happens when I try to add a game to a user (from console):

User.first.played_games << Game.first

Results in:

ActiveRecord::AssociationTypeMismatch: PlayedGame(#70279902258580) expected, got Game(#70279901145600)

Ok, maybe I'm misunderstanding. Perhaps it's games that I should try to add to:

User.first.games << Game.first.id

Results in:

NameError: uninitialized constant User::Games

Any help or link to documentation is appreciated. Thanks in advance!


原文:https://stackoverflow.com/questions/29859098
更新时间:2022-04-04 13:04

最满意答案

如果内置的FileDateTime()不是你以后可以使用FSO:

Dim FSO As Object: Set FSO = CreateObject("Scripting.FileSystemObject")
Dim objFile As Object: Set objFile = FSO.Getfile("C:\bleep\bloop.file")

Debug.Print "Date created: " & objFile.DateCreated
Debug.Print "Date last accessed: " & objFile.DateLastAccessed
Debug.Print "Date last modified: " & objFile.DateLastModified

如果它在扩展属性中的日期检查出来。


If the built in FileDateTime() isnt what your after you can use the FSO:

Dim FSO As Object: Set FSO = CreateObject("Scripting.FileSystemObject")
Dim objFile As Object: Set objFile = FSO.Getfile("C:\bleep\bloop.file")

Debug.Print "Date created: " & objFile.DateCreated
Debug.Print "Date last accessed: " & objFile.DateLastAccessed
Debug.Print "Date last modified: " & objFile.DateLastModified

If its a date in an extended attribute check this out.

相关问答

更多
  • 对于文本文件的最基本读取,请使用open 例: Dim FileNum As Integer Dim DataLine As String FileNum = FreeFile() Open "Filename" For Input As #FileNum While Not EOF(FileNum) Line Input #FileNum, DataLine ' read in data 1 line at a time ' decide what to do with datalin ...
  • 我们也可以不循环地执行此操作: Sub Test() Dim FSO As Object, MyFile As Object Dim FileName As String, Arr As Variant FileName = "C:\Test\Test.txt" ' change this to your text file full name Set FSO = CreateObject("Scripting.FileSystemObject") Set MyFile = FSO.OpenT ...
  • 您有大写分钟,即月,使用mm (和ss )代替: string dateCreation = fileCreatedDate.ToString("yyyyMMddHHmmss"); You have uppercase minutes which are months, use mm (and ss) instead: string dateCreation = fileCreatedDate.ToString("yyyyMMddHHmmss");
  • 我通过recording macro读取txt文件中的recording macro到excel中来尝试queryTable 我想你错过了一条关键路线 .RefreshStyle = xlOverwriteCells 我试过WITH和WITHOUT这一行,行为是替换和SHIFT数据到右边 RefreshStyle的默认值是xlInsertDeleteCells - Partial rows are inserted or deleted to match the exact number of rows r ...
  • 我创建了上传页面的副本并查找了错误的内容。 我发现页面重新边界是"myboundary" + ;Charset=UTF-8 。 在这种情况下的解决方案是在请求标头中添加charset。 With WinHttpReq 'UPLOAD REQUEST .Open "POST", sUrl, False .setRequestHeader "Content-Type", "multipart/form-data; Charset=UTF-8;boundary=" & STR_BOUNDARY .setRequ ...
  • 如果内置的FileDateTime()不是你以后可以使用FSO: Dim FSO As Object: Set FSO = CreateObject("Scripting.FileSystemObject") Dim objFile As Object: Set objFile = FSO.Getfile("C:\bleep\bloop.file") Debug.Print "Date created: " & objFile.DateCreated Debug.Print "Date last acce ...
  • 您正在捕获IOException,但您没有捕获或声明ParseException。 您需要捕获它或声明您的方法可能抛出它,因为它是一个经过检查的异常。 You are catching an IOException, but you are not catching, or declaring, the ParseException. You need to either catch it or declare that your method may throw it, as it is a checke ...
  • 这是我将如何做到这一点: Option Explicit Sub tester() Dim inputFilePath As String inputFilePath = "\\server\tsusers\Users.txt" MsgBox GetUserIpAddress("Chris Trucker", inputFilePath) ' or "JacksonC" or "Bozo" or whatever End ...
  • 最好的解决方案不是首先添加尾随逗号。 为此,您需要修改您没有向我们显示的代码。 或者,如果CSV创建代码是您不想触摸的黑色框,则可以手动删除字符串中的最后一个字符: myString = "a,b,c,d," If Right(myString, 1) = "," Then myString = Left(myString, Len(myString) - 1) End If ' myString is now "a,b,c,d" The best solution would be not ...
  • 'Defining text file variable Dim FilePath As String 'Text file path FilePath = path & "\OrderStatus.txt" Dim key As Variant 'Open the text file Open FilePath For Output As #1 With OrderStatus For Each key In .keys Write #1, key & “,” & .Item ...

相关文章

更多

最新问答

更多
  • python的访问器方法有哪些
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。
  • 响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
  • 在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
  • NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
  • 元素上的盒子阴影行为(box-shadow behaviour on elements)
  • Laravel检查是否存在记录(Laravel Checking If a Record Exists)
  • 设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
  • 想学Linux 运维 深圳有哪个培训机构好一点
  • 为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)
  • 在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)
  • AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)
  • 错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)
  • 等待EC2实例重启(Wait for an EC2 instance to reboot)
  • 如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)
  • 使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)
  • 江民杀毒软件的KSysnon.sys模块是什么东西?
  • 处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)
  • PerSession与PerCall(PerSession vs. PerCall)
  • C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)
  • 平衡一个精灵(Balancing a sprite)
  • n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的