我有一个奇怪的小Rails问题。想象一下从ActiveRecord :: Base继承的一个空的Invitation类。 sqlite3数据库中有一个邀请,但ActiveRecord无法通过其标记属性找到它。在我们尝试找到它的同时跟随它,看看你是否能找出原因。
0 urzatron work/sunrise % rails c
Loading development environment (Rails 3.1.0)
irb(main):001:0> Invitation.all
Invitation Load (0.5ms) SELECT "invitations".* FROM "invitations"
=> [#<Invitation id: 1, accepted_at: nil, assignment_id: 1, contact: "dean@example.net", network: "email", sender_id: 1, sent_at: "2011-11-07 02:50:10", token: "41a673223a2b059b212ca3753896d3fd38e1d1dd">]
irb(main):002:0> Invitation.find_by_token "41a673223a2b059b212ca3753896d3fd38e1d1dd"
Invitation Load (0.8ms) SELECT "invitations".* FROM "invitations" WHERE "invitations"."token" = '41a673223a2b059b212ca3753896d3fd38e1d1dd' LIMIT 1
=> nil
irb(main):003:0> Invitation.find 1
Invitation Load (82.7ms) SELECT "invitations".* FROM "invitations" WHERE "invitations"."id" = ? LIMIT 1 [["id", 1]]
=> #<Invitation id: 1, accepted_at: nil, assignment_id: 1, contact: "dean@example.net", network: "email", sender_id: 1, sent_at: "2011-11-07 02:50:10", token: "41a673223a2b059b212ca3753896d3fd38e1d1dd">
irb(main):004:0> Invitation.where( :token => "41a673223a2b059b212ca3753896d3fd38e1d1dd" ).first
Invitation Load (0.6ms) SELECT "invitations".* FROM "invitations" WHERE "invitations"."token" = '41a673223a2b059b212ca3753896d3fd38e1d1dd' LIMIT 1
=> nil
所以,这有点奇怪。
0 urzatron work/sunrise % rails dbconsole
SQLite version 3.7.5
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from invitations;
1||1|dean@example.net|email|1|2011-11-07 02:50:10.818984|41a673223a2b059b212ca3753896d3fd38e1d1dd
sqlite> select * from invitations where token = "41a673223a2b059b212ca3753896d3fd38e1d1dd";
sqlite>
这是好奇的迁移。
class CreateInvitations < ActiveRecord::Migration
def change
create_table :invitations do |t|
t.datetime :accepted_at
t.references :assignment
t.string :contact, :null => false
t.string :network, :null => false
t.references :sender, :null => false
t.datetime :sent_at
t.string :token, :null => false
end
add_index :invitations, :accepted_at
add_index :invitations, [:network,:token]
add_index :invitations, :token
add_column :users, :invitation_id, :integer
end
end
答案 0 :(得分:1)
我刚遇到同样的错误(我认为)。我使用SecureRandom.hex(8)
生成了令牌,无法执行* find_by_token *。
如果我用SecureRandom.urlsafe_base64(8)
替换令牌生成,则错误已修复。
有关详细信息,请参阅https://github.com/NoamB/sorcery/issues/19。
答案 1 :(得分:0)
所以,这是两件事之一:
答案 2 :(得分:0)
我昨天遇到了这个问题,结果发现将'token'类型更改为字符串而不是文本。一旦我这样做,SQLite3就可以正确地进行选择。
所以在迁移过程中(如果你愿意,可以改变),我还没有发布,所以我只是重新迁移了:
t.text :token
会改为
t.string :token
一切顺利。