Ruby on Rails的活动记录关联

编程入门 行业动态 更新时间:2024-10-24 00:25:52
本文介绍了Ruby on Rails的活动记录关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要2个型号为我的商店:

  • ShoeSize
  • 据我的概念 - 一只鞋子可以有多种尺寸(鞋子一个模型可以是34,35,36,37尺寸)

    我应该使用什么类型的关联?什么数据库表个行业,我需要创建以支持这些关联?

    下面是模型的一个例子中使用:

    Shoe.find(1).shoe_sizes => 34,35,36 ShoeSize(2).shoes => #Shoe1,#Shoe2,#Shoe5

    解决方案

    您应该使用 has_​​and_belongs_to_many 的关系。

    类鞋   has_and_belongs_to_many:shoe_sizes 结束 类ShoeSize   has_and_belongs_to_many:鞋 结束

    这是因为一个鞋可以有很多的鞋的尺寸,并同样一只鞋大小是对于不同的鞋可用

    I need 2 models for my store:

  • Shoe
  • ShoeSize
  • According to my concept - one shoe can have several sizes (one model of shoes can be 34,35,36,37 size).

    What types of associations should I use? What database table fileds do I need to create to support these associations?

    Here is an example of the models in use:

    Shoe.find(1).shoe_sizes => 34,35,36 ShoeSize(2).shoes => #Shoe1, #Shoe2, #Shoe5

    解决方案

    You should use a has_and_belongs_to_many relationship.

    class Shoe has_and_belongs_to_many :shoe_sizes end class ShoeSize has_and_belongs_to_many :shoes end

    This is because a Shoe can have many shoe sizes, and likewise one shoe size is available in for different shoes.

    更多推荐

    Ruby on Rails的活动记录关联

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

    发布评论

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

    >www.elefans.com

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