In the RoR guides, the following example of a polymorphic link is given everywhere:

class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end class Post < ActiveRecord::Base has_many :comments, :as => :commentable end class Picture < ActiveRecord::Base has_many :comments, :as => :commentable end 

It turns out that the posts (Post) and the pictures (Picture) have a lot of comments. And all the comments for example to some kind of picture can be obtained as

 @post.comments @picture.comments 

And how to organize such a link that one picture can have many comments and many posts? To be able to do such requests:

 @image.posts @image.comments 
  • Described correctly and the code was written incorrectly. Corrected, thanks - Pavel
  • Well, under the current description, two ordinary has_many in one model are perfect, what is the essence of the question? - D-side
  • Is the image really a comment? and one comment can belong to many images and posts? Or image is an image and it can have a lot of comments and many posts? We are ready to help, but you need to remove the contradiction in the last paragraph and the sample code. Your image belongs to image.pictures. - cheops
  • All the pictures in my database can be divided into 2 classes: the first class is the pictures that belong to the posts, the second class are the pictures that belong to the comments. I want to display all the pictures that belong to the comments. - Pavel
  • But the question is not written at all . And in response, it seems, something else is the third%) UPD: although not, secondly, there is order. But still, bring the issue in order . - D-side

2 answers 2

What you are describing in the question does not require a polymorph at all.

 rails g model Image file_url rails g model Post image:references body:text rails g model Comment image:references body:text rake db:migrate 

 class Image < ActiveRecord::Base has_many :posts has_many :images end 

And ... and everything. Just in Post and Comment image_id needed, and the usual has_many will work.


And getting all the pictures from the comments will look like this:

 Image.all.merge(Comment.joins(:image)) 

The request will be focused on comments, will rather sound like "take away all the pictures that are found in the comments." And since this will be an INNER JOIN , the pictures INNER JOIN in several comments will INNER JOIN up several times, so you will need to add .uniq .

  • can be distinct add =) - Mal Skrylev
  • @ MalSkrylev .uniq will do it :) - D-side 2:58 .uniq
  • although oil on canvas I did not see uniq distinct add - Mal Skrylev 15
  • @ Mal_Skrylev it will add DISTINCT to the query :) This is a feature of the AR query language. In a good way, .distinct more accurately reflects reality, simply .uniq more β€œidiomatic” for Ruby. - D-side
  • I understood, I did not see such a chip near the rail =) - Mal Skrylev

In response, based on your question and additional comments to it. Suppose we have the following three tables.

 create_table :comments, comment: "ΠšΠΎΠΌΠΌΠ΅Ρ‚Π°Ρ€ΠΈΠΈ" do |t| t.string :body, comment: "Π‘ΠΎΠ΄Π΅Ρ€ΠΆΠΌΠΎΠ΅" ... end create_table :posts, comment: "Новости" do |t| t.string :title, comment: "НазваниС" ... end create_table :pictures, comment: "Π˜Π·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΡ" do |t| t.string :path, comment: "НазваниС Ρ„Π°ΠΉΠ»Π°" t.integer :pictureable_id, comment: "ΠŸΠΎΠ»ΠΈΠΌΠΎΡ€Ρ„Π½Ρ‹ΠΉ внСшний ΠΊΠ»ΡŽΡ‡" t.string :pictureable_type, comment: "ΠŸΠΎΠ»ΠΈΠΌΠΎΡ€Ρ„Π½Ρ‹ΠΉ внСшний ΠΊΠ»ΡŽΡ‡" end 

There are three classes of them that implement the polymorphic connection of images with posts and comments.

 class Post < ActiveRecord::Base has_many :pictures, as: :pictureable end class Comment < ActiveRecord::Base has_many :pictures, as: :pictureable end class Picture < ActiveRecord::Base belongs_to :pictureable, polymorphic: true end 

Then you can extract all the images that belong to the comments as follows.

 Picture.where(pictureable_type: 'Comment') 
  • one
    Only this feedback is needed so that picture -> has_many: posts ,: comments - Mal Skrylev
  • Based on the comment: "All the pictures in my database can be divided into 2 classes: the first class is the pictures that belong to the posts, the second class are the pictures that belong to the comments. I want to display all the pictures that belong to the comments." - cheops