如果2way
表中名为posts
的数据库中的列名,
post = Post.first
post.2way
发生:
SyntaxError: unexpected tINTEGER, expecting '(
post.2way
^
尝试过Post.'2way'
仍然无效。
答案 0 :(得分:1)
列名不能以数字开头。因此,在定义迁移时,您可以执行以下操作:
t.integer '2way'
让我知道它是否有效。
答案 1 :(得分:1)
这将帮助您:
Post.last.send("2way")
Post.all.pluck("2way") #this will work if your column name in your migration file defined as a string in quotes, for example, t.integer '2way'
如果要设置一个值:
post.public_send('2way=', value)
让我知道它是否有效。
答案 2 :(得分:1)
正如NN796所说,您可以使用public_send
方法设置值:
post.public_send('2way=', value)
答案 3 :(得分:0)
您可以通过以下方式尝试:
post.try(:2way)