How do I model this relationship?

I have a post model . Each post has a title and many snippets .

class Post < ActiveRecord::Base
  has_many :snippets
end

class Snippet < ActiveRecord::Base
  belongs_to :post
  attr_accessible :position, :post_id
end

I want to have 4 different types of fragment ie:

  • body (text)
  • image (string)
  • code (text)
  • video (line)

Q1

Should I create four new models (called text, code, video, and image) that extend the snippet model as follows:

class Text < Snipppet
  attr_accessible :body
end

class Image < Snippet
  attr_accessible :image
end

class Video < Snippet
  attr_accessible :title
end

class Code < Snippet
  attr_accessible code
end

Q2

How can I refer to the contents of each fragment in my view, when each fragment can be one of 4 different things?

In my opinion, I would like to add something like this:

- for snippet in @post.snippets
  = place the content of the snippet here

Q3

, , . - , ?

+3
3

, , , , jystewart rails 3 act_as_polymorphs gem

:

, , , . 6 (post.rb, snippet.rb, code.rb. text.rb, image.rb video.rb)

  • .
  • .
  • , , - .

, , , 4- .

Single Table Inheritance, (, , ), , , , , , , , .

, . , , - , . . , 4- . . .

m.onkey.org - , , act_as_polymorphs.

, :

  • 6 , ActiveRecord:: Base
  • has_many_polymorphs
  • , "snippetable" .

:

class Post < ActiveRecord::Base    
  has_many_polymorphs :snippets, :from => [:codes, :texts, :videos, :images], :through => :snippets
end

class Snippet < ActiveRecord::Base
    belongs_to :snippetable, :polymorphic => true
end

class Code < ActiveRecord::Base
  # don't have to put anything in here  
end

class Text < ActiveRecord::Base
  # don't have to put anything in here          
end

class Video < ActiveRecord::Base
  # don't have to put anything in here  
end

class Image < ActiveRecord::Base
  # don't have to put anything in here  
end

, - CreateSnippets:

class CreateSnippets < ActiveRecord::Migration
  def self.up
    create_table :snippets do |t|
      t.references :post
      t.column :snippetable_id,   :integer, :null => false
      t.column :snippetable_type, :string,  :null => false

      t.timestamps
    end
  end
end

! , rails :

p = Post.first
p.codes << Code.create(:code=>"This is a code snippet")
p.images << Image.create(:image=>"xyz.png")
p.images << Image.create(:image=>"123.png")

p.codes.count # returns 1
p.images.count # returns 2
p.snippets.count # returns 3 !!!

Yaldi!

, 11 , , , . , -.

act_as_polymorph:

+2

type, . , , , , type .

, , , , .

, type , , .

, . snippet_content, type snippet_content_text snippet_content_image snippet_content_code snippet_content_video. if-then ( , ).

+5
  • , .

  • (texts/_text.html.haml, images/_image.html.haml ..), Rails :

    = render @post.snippets
    
  • : "type" - Rails .

Update: polymorphic relationships also work, let each fragment have its own tables. Is this a relationship or behaves like a type of relationship ? We can say that images and texts behave like fragments. In this case, go to the module with the name Snippetand mix it.

class Post < ActiveRecord::Base
  has_many :snippets, :polymorphic => true
end

class Text < ActiveRecord::Base
  include Snippet
  belongs_to :post, :as => :snippet
end

module Snippet
  # shared behavior here
end
+2
source

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


All Articles