2016-06-16 4 views
0

ActiveRecordで奇妙な動作に直面しています。まず、ここに私のデータベーススキーマです:ActiveRecordの奇妙な振る舞い "PG :: UndefinedColumn"(2人のユーザのテーブル)

enter image description here

、ここでは私のschema.rbファイルです:

ActiveRecord::Schema.define(version: 20160612080318) do 

    # These are extensions that must be enabled in order to support this database 
    enable_extension "plpgsql" 

    create_table "conversations", force: :cascade do |t| 
    t.integer "user1_id" 
    t.integer "user2_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    add_index "conversations", ["user1_id"], name: "index_conversations_on_user1_id", using: :btree 
    add_index "conversations", ["user2_id"], name: "index_conversations_on_user2_id", using: :btree 

    create_table "interests", force: :cascade do |t| 
    t.string "name" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    create_table "matches", force: :cascade do |t| 
    t.integer "user1_id" 
    t.integer "user2_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    add_index "matches", ["user1_id"], name: "index_matches_on_user1_id", using: :btree 
    add_index "matches", ["user2_id"], name: "index_matches_on_user2_id", using: :btree 

    create_table "messages", force: :cascade do |t| 
    t.text  "content" 
    t.integer "conversation_id" 
    t.integer "user_id" 
    t.datetime "read_at" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    end 

    add_index "messages", ["conversation_id"], name: "index_messages_on_conversation_id", using: :btree 
    add_index "messages", ["user_id"], name: "index_messages_on_user_id", using: :btree 

    create_table "user_interests", force: :cascade do |t| 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.integer "user_id" 
    t.integer "interest_id" 
    end 

    create_table "users", force: :cascade do |t| 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.inet  "current_sign_in_ip" 
    t.inet  "last_sign_in_ip" 
    t.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
    t.string "first_name" 
    t.string "last_name" 
    t.integer "age" 
    t.string "avatar_url" 
    end 

    add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree 
    add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree 

    add_foreign_key "messages", "conversations" 
    add_foreign_key "messages", "users" 
    add_foreign_key "user_interests", "interests" 
    add_foreign_key "user_interests", "users" 
end 

は私のモデルをテストするために、私は私のseed.rbファイルに以下の行を追加しました

sarah = User.create!(email: "[email protected]", password: "azertyuiop", first_name: "sarah", avatar_url: "http://i.imgur.com/jOGeCVC.jpg") 
    alexandre = User.create!(email: "[email protected]", password: "azertyuiop", first_name: "Alexandre", avatar_url: "http://i.imgur.com/2jFvkJu.jpg") 
    happbot = User.create!(email: "[email protected]", password: "azertyuiop", first_name: "Happbot", avatar_url: "https://pbs.twimg.com/profile_images/659236694575616000/aF21-Wxz.jpg") 
    lisa = User.create!(email: "[email protected]", password: "azertyuiop", first_name: "Lisa", avatar_url: "https://ph-avatars.imgix.net/175314/original?auto=format&fit=crop&crop=faces&w=220&h=220") 

    c1 = Conversation.create!(user1: yoann, user2: sarah) 
    c2 = Conversation.create!(user1: yoann, user2: alexandre) 
    c3 = Conversation.create!(user1: yoann, user2: happbot) 
    c4 = Conversation.create!(user1: sarah, user2: alexandre) 
    c5 = Conversation.create!(user1: sarah, user2: happbot) 
    c6 = Conversation.create!(user1: alexandre, user2: happbot) 
    c7 = Conversation.create!(user1: yoann, user2: lisa) 

    m1 = Message.create!(user: yoann, conversation: c1, content: "Hey") 
    m2 = Message.create!(user: yoann, conversation: c2, content: "Hello") 
    m3 = Message.create!(user: yoann, conversation: c3, content: "What's up?") 
    m4 = Message.create!(user: sarah, conversation: c1, content: "Salut Yoann") 
    m5 = Message.create!(user: alexandre, conversation: c2, content: "Yo!") 
    m6 = Message.create!(user: happbot, conversation: c3, content: "Tu peux pas test") 
    m7 = Message.create!(user: sarah, conversation: c4, content: "Salut alexandre") 
    m8 = Message.create!(user: sarah, conversation: c5, content: "Salut Happbot") 
    m9 = Message.create!(user: alexandre, conversation: c6, content: "Hey!") 
    m10 = Message.create!(user: alexandre, conversation: c4, content: "Hello") 
    m11 = Message.create!(user: happbot, conversation: c5, content: "Quoi de neuf ?") 
    m12 = Message.create!(user: happbot, conversation: c6, content: "Tu fais quoi demain ?") 
    m12 = Message.create!(user: yoann, conversation: c7, content: "Yo!") 

    i1 = Interest.create!(name: "books") 
    i2 = Interest.create!(name: "music") 
    i3 = Interest.create!(name: "coffee") 

    UserInterest.create!(user: yoann, interest: i1) 
    UserInterest.create!(user: yoann, interest: i2) 
    UserInterest.create!(user: yoann, interest: i3) 

    Match.create!(user1: yoann, user2: lisa) 

最初はすべてrake db:seedの場合はうまく動作しますが、一致を追加して別の作業を行う場合はrake db:seed私はこのエラーに遭遇します:

rake aborted! 
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column matches.user_id does not exist 
LINE 1: SELECT "matches".* FROM "matches" WHERE "matches"."user_id" ... 
               ^
HINT: Perhaps you meant to reference the column "matches.user1_id" or the column "matches.user2_id". 
: SELECT "matches".* FROM "matches" WHERE "matches"."user_id" = $1 
/Users/yolo/code/happenstance-app/db/seeds.rb:1:in `<top (required)>' 
-e:1:in `<main>' 
PG::UndefinedColumn: ERROR: column matches.user_id does not exist 
LINE 1: SELECT "matches".* FROM "matches" WHERE "matches"."user_id" ... 
               ^
HINT: Perhaps you meant to reference the column "matches.user1_id" or the column "matches.user2_id". 
/Users/yolo/code/happenstance-app/db/seeds.rb:1:in `<top (required)>' 
-e:1:in `<main>' 
Tasks: TOP => db:seed 
(See full trace by running task with --trace) 

ここに私のuser.rbファイルです:

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 
    has_many :user_interests, dependent: :destroy 
    has_many :interests, through: :user_interests, dependent: :destroy 
    has_many :messages, dependent: :destroy 
    has_many :matches, dependent: :destroy 

    def conversations 
    Conversation.includes(:messages) 
       .where("user1_id = :id OR user2_id = :id", id: id) 
       .order("messages.created_at DESC") 
    end 

    def other_user(conversation) 
    conversation.users.include?(self) ? conversation.other_user(self) : nil 
    end 

    def unread_conversations 
    conversations.select { |c| c.unread_messages?(self) } 
    end 

    def unread_conversations_count 
    unread_conversations.count 
    end 

    def unread_conversations? 
    unread_conversations_count > 0 
    end 

    def one_avatar_url 
    avatar_url ? avatar_url : "http://placehold.it/64x64" 
    end 
end 

私match.rbファイル:

class Match < ActiveRecord::Base 
    belongs_to :user1, class_name: "User" 
    belongs_to :user2, class_name: "User" 

end 

は、私は多くの答えをチェックしましたが、私は本当にと間違って何が表示されません私のスキーマまたは私のモデル。

ありがとうございました!

答えて

0

あなたはキー外国Userモデルでこのhas_many :matches, dependent: :destroyに使用するレールを伝える必要があります。デフォルトでは、このクラスの名前は小文字で、 "_id"は接尾辞付きであると推測されます。そのため、has_manyの関連付けを行うUserクラスは、:foreign_keyとしてuser_idを使用します。しかし、あなたはuser_idを持っていない、あなたはむしろuser1_idまたはuser2_idを持っています。

これは、このAR SQLからはっきりしています。

LINE 1: SELECT "matches".* FROM "matches" WHERE "matches"."user_id" ... 

私はあなたのユースケースを知らないが、どちらか一方が、この問題を解決します:

has_many :matches, dependent: :destroy, foreign_key: :user1_id 
# or 
has_many :matches, dependent: :destroy, foreign_key: :user2_id 
+1

ありがとうArup!確かにこの誤りは、間違いから来ました。私はまだ多くを学ぶ必要があります。あなたの貴重な助けをもう一度ありがとう! –

+0

@YoannLopezもしあなたが助けてくれたら、この回答に合格とマークしてください。 –

0

Matchモデルにforeign_keysを追加する必要があります。そのため、ActiveRecordはどのユーザーがどのforeign_keyを経由するかを認識します。

class Match < ActiveRecord::Base 
    belongs_to :user1, class_name: "User", foreign_key: "user1_id" 
    belongs_to :user2, class_name: "User", foreign_key: "user2_id" 

end 
+0

いいえ、これは、この問題を引き起こしていません。 –