Rails 3: creating a post model with different types of messages (image, link, text, video). Which legacy should I use?

I am building an application that allows users to create posts. Each message will have a type (image, video, link, text) with the same and unique variables. I want to present them through various forms in the "composer", available on the entire site.

Here is the publication model, Gallery:

class Gallery < ActiveRecord::Base attr_accessible :name, :asset has_attached_file :asset, :styles => { :small => '160x120#', :medium => "500x", :thumb => "300x200#" } belongs_to :user has_many :likes validates :user_id, presence: true validates :type, presence: true validates :name, presence: true, length: { maximum: 140, minimum: 1 } end 

I was thinking about using Single Table Inheritance, for example:

 class Image < Gallery end class Link < Gallery end class Video < Gallery end class Text < Gallery end 

But I did not achieve the result I want. First, I would like to use methods in my Gallery controller, for example:

 # galleries_controller.rb def like @gallery = Gallery.find(params[:id]) @like = @gallery.likes.build(:user_id => current_user.id) respond_to do |format| if @like.save format.html { redirect_to @gallery } format.js end end 

end

In addition, I want to create a β€œpublisher” form, which contains a form for each type of message and allows you to create any message.

How would you approach this? I am new to Rails and I want to take advantage of all the amenities that it offers. Really appreciate!

+4
source share
2 answers

I would make Gallery an abstract class, and each subclass has a separate db table. This will allow subclasses to have unique attributes as well as common ones.

 class Gallery < ActiveRecord::Base self.abstract_class = true # very important . . end 

I would make the gallery controller polymorphic:

 class GalleriessController < ApplicationController before_filter :find_gallery private def find_gallery @klass = params[:gallery_type].capitalize.constantize @gallery = @klass.find(params[:gallery_id]) end end 

Views:

You can use form_for @gallery, but a little massage is needed to work with the polymorphic controller described above. It will have to present the gallery_type and gallery_id parameters, not just the identifier. You can change form_for: url param for this or with hidden_fields. Inside form_for, you can use partials, one for each subclass.

Routes

You can define a regular resource, for example: resources: galleries you will need, however, for the id parameter to be gallery_id (and also always pass the galley_type parameter). Check this out: Changing the id parameter in Rails routing

(possibly) conclusion :

Sometimes bizarre polymorphic things will work well, but in other cases a simpler approach with brute force may make better sense: have a controller for each subclass. It depends on situation. If you end up doing too many things that the Rails framework doesn't give you for free, it might be better to consider a different approach.

0
source

I had a similar scenario, and looking at a good way to implement it, I found this stone that I use:

https://github.com/hzamani/acts_as_relation

It uses the has_one polymorphic association to simulate inheritance with multiple tables, so in your case you can do the following:

 class Gallery < ActiveRecord::Base acts_as_superclass ... end class Image < ActiveRecord::Base acts_as :gallery ... end class Link < ActiveRecord::Base acts_as :gallery ... end 

Hope this helps you. Hooray!

0
source

Source: https://habr.com/ru/post/1439883/


All Articles