我有一個奇怪的小Rails問題。想象一下從ActiveRecord :: Base繼承的空的Invitation類。在sqlite3數據庫中有一個邀請,但ActiveRecord無法通過其令牌屬性找到它。跟着我們試圖找到它,看看你是否能找出原因。sqlite3無法通過屬性找到記錄
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: "[email protected]", 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: "[email protected]", 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|[email protected]|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
而你的問題是...? –
什麼'選擇' - '||令牌|| ' - '從邀請'或'選擇邀請函中的長度(令牌)'必須從'rails dbconsole'說? –
'select' - '||令牌|| ' - ' 從邀請;'=> ** - 41a673223a2b059b212ca3753896d3fd38e1d1dd - ** '選擇長度(令牌)從邀請;'=> ** 40 ** '選擇長度( 「41a673223a2b059b212ca3753896d3fd38e1d1dd」)'=> * * 40 ** –