activerecord - Ruby on Rails Associate pairs of one model to a second model -


i'm building rails app have models users, images, , image_pairs. want each image_pair have 2 images, named :before_image , :after_image.

so:

  • users have many images (many one)
  • users have many image_pairs (many one)
  • images may have 1 image_pair
  • image_pairs have 2 images

i have working, except can't call @image.image_pair. get:

pg::undefinedcolumn: error: column image_pairs.image_id not exist

how can set can image_pair image?

schema.rb (some irrelevant fields removed)

create_table "image_pairs", force: true |t|   t.integer  "before_image_id"   t.integer  "after_image_id" end  add_index "image_pairs", ["before_image_id"], name: "index_image_pairs_on_before_image_id", using: :btree add_index "image_pairs", ["after_image_id"], name: "index_image_pairs_on_after_image_id", using: :btree add_index "image_pairs", ["user_id"], name: "index_image_pairs_on_user_id", using: :btree  create_table "images", force: true |t|   t.integer  "user_id"   t.integer  "image_pair_id" end  add_index "images", ["image_pair_id"], name: "index_images_on_image_pair_id", using: :btree add_index "images", ["user_id"], name: "index_images_on_user_id", using: :btree  create_table "users", force: true |t|   t.string   "name" end 

user.rb

class user < activerecord::base   has_many :images, dependent: :destroy   has_many :image_pairs, dependent: :destroy end 

image.rb

class image < activerecord::base   belongs_to :user   has_one :image_pair end 

image_pair.rb

class imagepair < activerecord::base   belongs_to :user   belongs_to :before_image, :class_name => "image"   belongs_to :after_image, :class_name => "image" end 

you might add field image_pairs called image_id points original image id. or rename before_image image_id , add alias or method if want call name before_image.

def before_image   self.image_id end 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -