Ruby on Rails中的多对多关联(Many to many polymorphic association in Ruby on Rails)

编程入门 行业动态 更新时间:2024-10-25 01:37:04
Ruby on Rails中的多对多关联(Many to many polymorphic association in Ruby on Rails)

Video , Song和Article可以有很多Tags 。 每个Tag也可以有很多Video, Songs or Articles 。 所以我有5个模型: Video, Song, Article, Tag and Taggings 。

以下是这些模型:

class Video < ActiveRecord::Base has_many :tags, :through => :taggings end class Song < ActiveRecord::Base has_many :tags, :through => :taggings end class Article < ActiveRecord::Base has_many :tags, :through => :taggings end class Tag < ActiveRecord::Base has_many :articles has_many :videos has_many :songs belong_to :taggings, :polymorphic => true #is this correct? end

Taggings的数据库定义

create_table "taggings", :force => true do |t| t.integer "tag_id" t.string "taggable_type" t.integer "taggable_id" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end

Taggings模型:

class Taggings < ActiveRecord::Base belongs_to :tag #is this correct? belong_to :taggable, :polymorphic => true #is this correct? end

我担心的问题是,我有正确的模型定义( belongs_to , has_many ?)吗? 我的直觉告诉我,我错过了一些东西。 我看过很多文章,我很困惑。

A Video, a Song and an Article can have many Tags. And each Tag also can have many Video, Songs or Articles. So I have 5 models: Video, Song, Article, Tag and Taggings.

Here are these models:

class Video < ActiveRecord::Base has_many :tags, :through => :taggings end class Song < ActiveRecord::Base has_many :tags, :through => :taggings end class Article < ActiveRecord::Base has_many :tags, :through => :taggings end class Tag < ActiveRecord::Base has_many :articles has_many :videos has_many :songs belong_to :taggings, :polymorphic => true #is this correct? end

The database definition of Taggings

create_table "taggings", :force => true do |t| t.integer "tag_id" t.string "taggable_type" t.integer "taggable_id" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end

Taggings model:

class Taggings < ActiveRecord::Base belongs_to :tag #is this correct? belong_to :taggable, :polymorphic => true #is this correct? end

The issue I'm worried by is, do I have right definitions of model (belongs_to, has_many?) ? My gut tells me that I missed something. I've seen many articles and I'm quite confused by them.

最满意答案

您需要这些更改:

class Video < ActiveRecord::Base # or Song, or Article has_many :taggings, :as => :taggable # add this has_many :tags, :through => :taggings # ok class Tag < ActiveRecord::Base # WRONG! Tag has no tagging_id # belong_to :taggings, :polymorphic => true has_many :taggings # make it this way # WRONG! Articles are available through taggings # has_many :articles # make it this way with_options :through => :taggings, :source => :taggable do |tag| tag.has_many :articles, :source_type => 'Article' # same for videos # and for songs end

关于with_options 。

除了它的名字,你的班级Taggings似乎没问题。 它必须是单数, Tagging :

class Tagging < ActiveRecord::Base # no 's'! belongs_to :tag belong_to :taggable, :polymorphic => true end

You need these changes:

class Video < ActiveRecord::Base # or Song, or Article has_many :taggings, :as => :taggable # add this has_many :tags, :through => :taggings # ok class Tag < ActiveRecord::Base # WRONG! Tag has no tagging_id # belong_to :taggings, :polymorphic => true has_many :taggings # make it this way # WRONG! Articles are available through taggings # has_many :articles # make it this way with_options :through => :taggings, :source => :taggable do |tag| tag.has_many :articles, :source_type => 'Article' # same for videos # and for songs end

About with_options.

Your class Taggings seems ok except its name. It has to be singular, Tagging:

class Tagging < ActiveRecord::Base # no 's'! belongs_to :tag belong_to :taggable, :polymorphic => true end

更多推荐

本文发布于:2023-07-05 01:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1031268.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Rails   Ruby   association   polymorphic

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!