최근 레일스를 배우기 시작했습니다.레일에 잘못된 값으로 레코드를 삽입 할 때 뭔가 잘못되었습니다. 3.2
이제 내 자신의 프로젝트에서 우정 관계를 구현하고 있습니다. 그리고 여기 코드는 그런 어떤 것을 (그들 중 일부는 인터넷에서 수집)
class Friendship < ActiveRecord::Base
attr_accessible :friend_id, :message, :user_id, :approved
validates_uniqueness_of :user_id, :scope => [:friend_id]
validates :approved, :presence => true
belongs_to :user
belongs_to :friend, :class_name => "User", :foreign_key => "friend_id"
end
/////////////////////
class User < ActiveRecord::Base
has_many :friendships
has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
has_many :direct_friends, :through => :friendships, :conditions => { 'friendships.approved' => true }, :source => :friend
end
입니다
//////////////////////
class CreateFriendships < ActiveRecord::Migration
def change
create_table :friendships do |t|
t.integer :user_id
t.integer :friend_id
t.text :message
t.boolean :approved, :default => false
t.timestamps
end
end
내가 콘솔에서 새로운 친구를 만들 "레일 C는"이 확인 될 때 approved = true
Friendship.create(:user_id=>7, :friend_id=>11, :approved=>1, :message=>'Be my friend :D')
(0.1ms) begin transaction
SQL (0.9ms) INSERT INTO "friendships" ("approved", "created_at", "friend_id", "message", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?) [["approved", true], ["created_at", Sat, 13 Oct 2012 14:15:36 UTC +00:00], ["friend_id", 11], ["message", "Be my friend :D"], ["updated_at", Sat, 13 Oct 2012 14:15:36 UTC +00:00], ["user_id", 7]]
(12.2ms) commit transaction
하지만 난 = 거짓의 승인을 설정하면, 아래
Friendship.create(:user_id=>6, :friend_id=>7, :approved=>0)
(0.1ms) begin transaction
(0.1ms) rollback transaction
=> #<Friendship id: nil, user_id: 6, friend_id: 7, message: nil, approved: false, created_at: nil, updated_at: nil>
처음이다. 나는 데이터베이스 (sqlite3를) 문제라고 생각했다,하지만이 시도하면, sqlite3를 터미널에서, 그것은
sqlite> insert into friendships (user_id, friend_id, created_at, updated_at) values(11, 6, '2012-10-13 14:15:36', '2012-10-13 14:15:36');
sqlite> select * from friendships;
id = 1
user_id = 11
friend_id = 6
message =
approved = f
created_at = 2012-10-13 14:15:36
updated_at = 2012-10-13 14:15:36
당신이 일이 어떻게 그것을 해결하는 방법을 말해 줄 수 좋았어요.
을 그것은 아마도 검증 함께 할 수있는 뭔가가. 유효성 검사를 제거하면됩니다. 그것은 어쨌든 쓸모가 없다. – Mischa