ror 通过主键查找显示外键

xiaoxiao2026-01-14  4

在数据库中定义了两张表,products 和candidate_keywords.主表为products,和candidate_keywords有一对多的关系现在希望在product类的视图中显示和当前条目相关的多条外键的某字段的值 1在创建表的时候要设置外键,外键和主键命名有一定的约定,作为外键的字段命名为主表名_id 2,创建模型,在从表模型中声明has_a :主表的类(单数),主表模型声明belongs_to:从表名(复数) 3p><label for="product_candidate">Candidate</label><br/> <% for candidate_keyword in @product.candidate_keywords %> <td><%=candidate_keyword.tag %></td> 类似的形式,通过主表模型的product.candidate_keywords找出所有的和当前主键关联的从表对象,将这些对象存在对象数组中,依次遍历数组中的元素,显示从表对象的特定字段 学习的心得 <% form_tag :action => 'update', :id => @product do %> <%= submit_tag 'Edit' %> <% end %> 组成一个提交页面 相关资源:敏捷开发V1.0.pptx
转载请注明原文地址: https://www.6miu.com/read-5042618.html

最新回复(0)