Ruby on rails 在Rails中的两个模型之间建立一对一和一对多关系

Ruby on rails 在Rails中的两个模型之间建立一对一和一对多关系,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,因此,如果您有两个模型,比如Post和Tag,您希望有一个“主要”标记和许多“次要”标记,所有这些都来自于标记模型。。。这可能吗?我可以想象连接-您需要在Post模型中有一个主标记ID列和一个用于辅助标记的连接表。。。但是在Rails中有什么简单的方法可以做到这一点吗 谢谢 这是我能想到的唯一方法,这很简单,有很多,通过:根据需要选择 class Post < ApplicationRecord has_one :tag has_many :secondary_tags has_ma

因此,如果您有两个模型,比如Post和Tag,您希望有一个“主要”标记和许多“次要”标记,所有这些都来自于标记模型。。。这可能吗?我可以想象连接-您需要在Post模型中有一个主标记ID列和一个用于辅助标记的连接表。。。但是在Rails中有什么简单的方法可以做到这一点吗


谢谢

这是我能想到的唯一方法,这很简单,有很多,通过:根据需要选择

class Post < ApplicationRecord
 has_one :tag
 has_many :secondary_tags
 has_many :tags, through: :secondary_tags
end

class Tag < ApplicationRecord
 belongs_to :post
 has_many :secondary_tags     
 has_many :posts, through: :secondary_tags
end

class SecondaryTags < ApplicationRecord
 belongs_to :post
 belongs_to :tag
end
按如下方式创建联接表:

class CreateSecondaryTags < ActiveRecord::Migration[5.0]
  def change
    create_table :secondary_tags do |t|
      t.integer :post_id
      t.integer :tag_id

      t.timestamps
    end
    add_index :secondary_tags, :post_id
    add_index :workout_categories, :tag_id
  end
end

这是我能想到的唯一方法,这很简单,有很多,通过:根据需要选择

class Post < ApplicationRecord
 has_one :tag
 has_many :secondary_tags
 has_many :tags, through: :secondary_tags
end

class Tag < ApplicationRecord
 belongs_to :post
 has_many :secondary_tags     
 has_many :posts, through: :secondary_tags
end

class SecondaryTags < ApplicationRecord
 belongs_to :post
 belongs_to :tag
end
按如下方式创建联接表:

class CreateSecondaryTags < ActiveRecord::Migration[5.0]
  def change
    create_table :secondary_tags do |t|
      t.integer :post_id
      t.integer :tag_id

      t.timestamps
    end
    add_index :secondary_tags, :post_id
    add_index :workout_categories, :tag_id
  end
end

在您的情况下,需要一个联接表post_标记。此表具有列primary以指示它是否是主标记。模型应如下所示:

class PostTag
  belongs_to :tags
  belongs_to :posts

  # it has primary with type is boolean
end

class Post
  has_many  :post_tags
  has_one   :primary_tag,   -> { where("post_tags.primary": true) }, through: :post_tags, class_name: "Tag"
  has_many  :secondary_tags, -> { where("post_tags.primary": false) }, through: :post_tags, class_name: "Tag"
end

class Tag
  has_many  :post_tags
  has_many  :posts, through: :post_tags
end

在您的情况下,需要一个联接表post_标记。此表具有列primary以指示它是否是主标记。模型应如下所示:

class PostTag
  belongs_to :tags
  belongs_to :posts

  # it has primary with type is boolean
end

class Post
  has_many  :post_tags
  has_one   :primary_tag,   -> { where("post_tags.primary": true) }, through: :post_tags, class_name: "Tag"
  has_many  :secondary_tags, -> { where("post_tags.primary": false) }, through: :post_tags, class_name: "Tag"
end

class Tag
  has_many  :post_tags
  has_many  :posts, through: :post_tags
end

您好,谢谢您的回答-这看起来是更直接的解决方案-但我有一个问题-如果我得到Tag.all-如果有人有一个主标记和一个同名的辅助标记,那么标记不会在Tag.all中出现两次吗?我真正想做的是在Post端建立主/辅连接,这样标记就可以用作主标记或辅标记。。但它只会在标记表中出现一次…不,它不会出现两次。标记可以在post_tags表上多次出现,因为它可以是主标记和辅助标记。但是它在标签表中只显示一个,因此调用tag.all将只返回一个标签。非常感谢!您好,我只是因为一些其他问题才尝试设置它-它是错误的,在模型PostTag中找不到源关联primary_标记或:primary_标记。尝试“has_many:primary_标记,:through=>:note_标记,:source=>”。是便条还是标签?有什么想法吗?@PaulHarker:你能试着改变一下:class_name:Tag to source::Tag on primary_Tag和secondary_Tag?嗨,谢谢你的回答-这看起来是更直截了当的解决方案-但我有一个问题-如果我得到Tag.all-如果有人有一个主标记和一个同名的secondary标记,标签不会出现在tag.all中两次吗?我真正想做的是在Post端建立主/辅连接,这样标记就可以用作主标记或辅标记。。但它只会在标记表中出现一次…不,它不会出现两次。标记可以在post_tags表上多次出现,因为它可以是主标记和辅助标记。但是它在标签表中只显示一个,因此调用tag.all将只返回一个标签。非常感谢!您好,我只是因为一些其他问题才尝试设置它-它是错误的,在模型PostTag中找不到源关联primary_标记或:primary_标记。尝试“has_many:primary_标记,:through=>:note_标记,:source=>”。是便条还是标签?有什么想法吗?@PaulHarker:你能试着在主标签和次标签上把class\u name:Tag改成source::Tag吗?