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

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

问题描述

我在Rails中是新的,正在处理一个问题。我有两张桌子:



鞋子和袜子



鞋子可以有很多袜子,但只有一个活跃的袜子。其他袜子无效。所有的袜子也是独特的独特的模式。我想确保我没有相同模式的袜子。我想我可以这样做三种方式



1)在表袜子中使用一个附加列表示活动袜子

  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
结束
结束

这看起来很简单,但很麻烦。我会用shoe_id搜索袜子,并拉出active_sock并返回它的模式。我想我将索引一个数组中的[active_sock,shoe_id]来加快它的速度。



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

  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

这似乎很麻烦,但是当你只是处理活动袜子易于使用和快速。但是在购买新的袜子时,我必须用两张表来检查图案。



3)使用has_many:通过关系

  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。时间戳null:false
end
end
end

像选项2,但我觉得我正在使用Rails工具,使它不那么麻烦。当我正在搜索模式时,我只是检查socks表,当我正在搜索一个active_sock时,我只是在搜索active_socks表。



在类似的帖子中已经阅读过,似乎选项1和2通常用在closed_accounts中,禁止用户,禁止帖子,归档等。需要区分仅略有不同的数据的情况。那里的选择似乎是看你所需要的,并选择最适合你的选项1或2。



对于has_many通过情况的理解似乎是当你有一个关系,你需要额外的元数据,你可以使用它。我认为这适合这种情况。



我是否正确设置了选项1,我是否正确地编制[shoe_id和active]的数组会给我一个更快的搜索?选项3是否适合使用has_many?我对选项3的解释是否有效?

解决方案

你的使用方式是什么?我猜你只是希望能够找到活跃的袜子给一个鞋子,如果一个给定的袜子是活跃的或不活跃的。为了快速找到给定鞋子的活跃袜子,您只需要使用 belongs_to 关联将袜子给外套钥匙。

  class Sock< ActiveRecord :: Base 
belongs_to:shoe
end

并找出是否袜子是活跃的或不活跃的,给其主人鞋参考其活跃的袜子,如下所示:

  class Shoe< ActiveRecord :: Base 
belongs_to:sock
end

现在,你可以去鞋的鞋子,检查鞋子的活动袜子是否是当前的袜子。例如。

  def is_active 
owner_shoe.active_sock == self

基本上,将他们与外键相关联,你很好去!
P.S.您的复数袜子,但Rails约定是使用单数形式的名称。



编辑:您要求迁移,所以这里是一个上面的代码。注意事项:我没有在Rails中长时间进行迁移,所以有可能会关闭。

  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做| t |
t.belongs:owner_shoe,foreign_key:shoe_id
t.string:pattern
end
end
end


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

Shoes and Socks

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) 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

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) 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) 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

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.

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.

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.

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?

解决方案

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

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

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

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.

EDIT: 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协会已经通过禁令/归档解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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