Rails 协会 has_many 通过禁止/归档解决方案? [英] Rails associations has_many through ban/archive solution?

查看:23
本文介绍了Rails 协会 has_many 通过禁止/归档解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rails 新手,正在解决一个问题.我有两个表:

I'm new in Rails and am working on a problem. I have two tables:

鞋子和袜子

一只鞋子可以有很多袜子,但只有一个有效的袜子.其他袜子不活动.所有袜子也都是独一无二的,具有独特的图案.我想确保我没有相同图案的袜子.我想我可以通过三种方式做到这一点

A Shoe can have many Socks, but only one active Sock. Other Socks are inactive. All Socks are also unique with unique patterns. I want to make sure I don't have socks with the same pattern. I think I can do this three ways

1) 使用表socks中的附加列来表示活动sock

1) Using an additional column in table socks to represent the active sock

class Shoe < ActiveRecord::Base
  has_many :socks
end

class Sock < ActiveRecord::Base
  belongs_to :shoe
end

class CreateGettingDressed < ActiveRecord::Migration
  def change
    create_table :shoes do |t|
      t.string :size
      t.timestamps null: false
    end

    create_table :socks do |t|
      t.belongs_to :shoe, index:true
      t.string :pattern
      t.boolean :active
      t.timestamps null: false
    end
  end
end

这看起来相当简单,但很麻烦.我会用shoe_id搜索袜子,然后拉出active_sock并返回它的模式.我想我会在数组中索引 [active_sock, shoes_id] 以加快速度.

This seems fairly simple, but cumbersome. I would search socks with shoe_id, and pull out the active_sock and return it's pattern. I think I would index [active_sock, shoe_id] in an array to speed it up.

2) 使用附加表来归档不活动的袜子

2) Using an additional table to archive inactive socks

class Shoe < ActiveRecord::Base
  has_many :socks
  has_many :inactive_socks
end

class Sock < ActiveRecord::Base
  belongs_to :Shoe
end

class Inactive_sock < ActiveRecord::Base
  belongs_to :Shoe
end

class CreateGettingDressed < ActiveRecord::Migration
  def change
    create_table :shoes do |t|
      t.string :name
      t.timestamps null: false
    end

    create_table :socks do |t|
      t.belongs_to :shoe, index:true
      t.string :pattern
      t.timestamps null: false
    end


    create_table :inactive_socks do |t|
      t.belongs_to :shoe, index:true
      t.string :pattern
      t.timestamps null: false
    end
  end
end

这看起来也很麻烦,但是当您只处理活动袜子时,它易于使用且速度快.但是买新袜子时,我必须用两张桌子检查图案.

This seems cumbersome as well, but when you are just dealing with active socks easy to use and fast. But when buying a new sock, I have to check the pattern with both tables.

3) 使用 has_many :through 关系

3) Using a has_many :through relationship

class Shoe < ActiveRecord::Base
  has_many :active_socks
  has_many :socks, through: active_socks 
end

class Active_Sock < ActiveRecord::Base
  belongs_to :Shoe
  belongs_to :Sock
end

class Sock < ActiveRecord::Base
  has_many :active_socks
  has_many :shoes, through: active_socks
end

class CreateGettingDressed < ActiveRecord::Migration
  def change
    create_table :shoes do |t|
      t.string :name
      t.timestamps null: false
    end

    create_table :socks do |t|
      t.string :pattern
      t.timestamps null: false
   end

    create_table :active_socks do |t|
      t.belongs_to :shoe, index: true
      t.belongs_to :sock, index: true
      t.string :pattern
      t.boolean :active
      t.timestamps null: false
    end
  end
end

这似乎是选项 2,但我觉得我正在使用 Rails 工具来使它不那么麻烦.当我搜索模式时,我只是在检查袜子表,当我在搜索 active_sock 时,我只是在搜索 active_socks 表.

This seems like option 2, but I feel like I'm using Rails tools to make it less cumbersome. When I'm searching for patterns I'm just checking the socks table, when I'm searching for the one active_sock I'm just searching active_socks table.

我读过类似的帖子,似乎选项 1 和 2 常用于 closed_accounts、禁止用户、禁止帖子、存档等.您需要区分仅略有不同的数据的情况.那里的选择似乎是看你需要什么,然后选择最适合你的选项 1 或 2.

I've read up on similar posts, and it seems options 1 and 2 are commonly used in closed_accounts, banning users, banning posts, archiving etc. Situations where you need to differentiate data that is only slightly different. The choice there seems to be look at what you need and choose the option 1 or 2 that best fits you.

我对 has_many 的理解似乎是当你有关系并且你需要额外的元数据时可以使用它.我认为这适合这种情况.

My understanding for has_many through situations seems to be when you have a relationship and you need extra meta data you can use it. I think that fits this situation.

我是否正确设置了选项 1,我对 [shoe_id 和 active] 数组的索引是否会加快搜索速度?选项 3 是否适合使用 has_many through?我对选项 3 的解释是否有效?

Did I set up option 1 correctly and am I right that indexing the array of [shoe_id and active] will give me a faster search? Is option 3 an appropriate use of has_many through? Would my explanation of option 3 work?

推荐答案

你的使用模式是什么?我猜你只是希望能够找到给定鞋子的活动袜子,以及给定的袜子是活动的还是非活动的.要快速找到给定 Shoe 的活动 Sock,您只需使用 belongs_to 关联为 Sock 指定一个指向它的 Shoe 的外键.

What are your usage patterns? I'm guessing you just want to be able to find the active Sock given a Shoe, and if a given Sock is active or inactive. To quickly find the active Sock of a given Shoe, you merely need to give the Sock a foreign key to its Shoe with the belongs_to association.

class Sock < ActiveRecord::Base
  belongs_to :shoe
end

要确定 Sock 是活动的还是非活动的,请像这样向其所有者的鞋子提供对其活动袜子的引用:

And to find out if a Sock is active or inactive, give its owner shoe a reference to its active sock like so:

class Shoe < ActiveRecord::Base
  belongs_to :sock
end

现在,您可以前往其所有者 Shoe 并检查该 Shoe 的活动袜子是否是当前的 Sock.例如.

Now, you can just go to its owner Shoe and check if the Shoe's active sock is the current Sock or not. E.g.

def is_active
  owner_shoe.active_sock == self

基本上,将它们与外键相关联,您就可以开始了!附言您将 Socks 使用了复数形式,但 Rails 约定使用单数形式作为模型名称.

Basically, associate them with foreign keys and you're good to go! P.S. you pluralized Socks but the Rails convention is to use singular for model names.

您要求进行迁移,因此这是上面代码的迁移.警告:我已经很长时间没有在 Rails 中进行迁移了,所以可能会出现问题.

You asked for a migration so here's one for the code above. Caveat: I haven't done migrations in a long time in Rails so something might be off.

class CreateGettingDressed < ActiveRecord::Migration
  def change
    create_table :shoes do |t|
      t.belongs_to :active_sock, foreign_key: "sock_id"
      t.string :size
      t.timestamps null: false
    end

    create_table :socks do |t|
      t.belongs :owner_shoe, foreign_key: "shoe_id"
      t.string :pattern
    end
  end
end

这篇关于Rails 协会 has_many 通过禁止/归档解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆