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
最满意答案
如果内置的
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 ...
-
获取文本文件的日期创建(Get date creation of a text file)[2021-12-03]
您有大写分钟,即月,使用mm (和ss )代替: string dateCreation = fileCreatedDate.ToString("yyyyMMddHHmmss"); You have uppercase minutes which are months, use mm (and ss) instead: string dateCreation = fileCreatedDate.ToString("yyyyMMddHHmmss"); -
通过VBA导入文本文件(Import Text File by VBA)[2022-06-08]
我通过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 ... -
Vba上传文本文件(Vba Upload Text File)[2023-05-09]
我创建了上传页面的副本并查找了错误的内容。 我发现页面重新边界是"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 ...
-
VBA文本文件搜索(VBA text file search)[2022-11-24]
这是我将如何做到这一点: 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 ...