Yellow Fish
Yellow Fish

Reputation: 13

Rails - how to handle multiple associations the right way

I'm currently learning rails by working on a little personal project (best way to learn!) and I have some problems with models associations. After a day of searching I wasn't able to find answers to my questions.

I have 3 things to manage: posts (like in a blog), galleries and images. What I want to do is :

Edit: I don't upload images when I create (or edit) a post or a gallery. Basically, when I create (or edit) a post or a gallery, I juste want to display all the images stored in the db and select which image I want to add in a gallery (or as a post's main_image.)

I'm trying to figure out what is the best way to do it.

What I have now (and it works) is 3 models for each data (Post, Gallery and Image) and 3 models for join tables (PostsGallery, PostsImage and GalleriesImage.)

models/post.rb

class Post < ActiveRecord::Base
  has_many :posts_images
  has_many :images, through: :posts_images
  has_many :posts_galleries
  has_many :galleries, through: :posts_galleries

models/gallery.rb

class Gallery < ActiveRecord::Base
  has_many :galleries_images
  has_many :images, through: :galleries_images
  has_many :posts_galleries

models/image.rb

class Image < ActiveRecord::Base
  has_many :galleries_images
  has_many :posts_images

And for the join tables:

models/posts_gallery.rb

class PostsGallery < ActiveRecord::Base
  belongs_to :post
  belongs_to :gallery

models/posts_image.rb

class PostsImage < ActiveRecord::Base
  belongs_to :post
  belongs_to :image

models/galleries_image.rb

class GalleriesImage < ActiveRecord::Base
  belongs_to :gallery
  belongs_to :image

I wasn't able to create a has_one assiociation to link only one image to the post model, same with the galleries. I don't know where I failed, but I had errors. So I had to use some tricks in the code to store only one entry for the post main image, and for the post gallery. It's not great, but it works and I will do with that for the time being.

With the code I have in my controllers, when I remove an image, the entries in the join tables (galleries_images and posts_images) are removed too. Same when I remove a gallery, the entries in the galleries_images and posts_galleries tables are removed, etc.

Everything works like I want, but I don't know if this is a nice way to do it, and I'm still trying to find another way. I've looked at the polymorphic associations but I wasn't able to find an explanation on how to do it with my requirements.

If someone has an idea and can explain a (the?) good way of doing what I want to do, I would really appreciate it!

Thanks to anyone who read this post!

Upvotes: 0

Views: 100

Answers (1)

Kh Ammad
Kh Ammad

Reputation: 1085

There is no need of joining models, please define your association like that:

post.rb

  has_one :main_image, class_name: Image, as :imagesable.
  belongs_to :gallery

gallery.rb

  has_many :images, as: imageable

image.rb (Polymorphic)

  belongs_to :imageable, polymorphic: true

To learn more about assocaition, please follow this link

Upvotes: 1

Related Questions