Rails关联has_many:through [英] Rails associations has_many :through

查看:234
本文介绍了Rails关联has_many:through的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Ruby on Rails,并且遇到了has_many :through关联问题.

I'm getting started with Ruby on Rails and I have encountered an issue with the has_many :through association.

我正在使用的模型是:

class Phrase < ActiveRecord::Base
  attr_accessible :event_type_id, :template_pieces

  belongs_to :event_type
  has_many :phrases_pieces
  has_many :template_pieces, :through => :phrases_pieces
end

class TemplatePiece < ActiveRecord::Base
  attr_accessible :datatype, :fixed_text, :name

  has_many :phrase_pieces
  has_many :phrases, :through => :phrases_pieces
end

class EventType < ActiveRecord::Base
  attr_accessible :name

  has_many :phrases
end

class PhrasesPiece < ActiveRecord::Base
  attr_accessible :order, :phrase_id, :template_piece_id

  belongs_to :phrase
  belongs_to :template_piece
end

我正在尝试创建一个新短语,将其默认形式编辑为:

And I'm trying to create a new phrase, editing its default form to:

<%= form_for(@phrase) do |f| %>
  <% if @phrase.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@phrase.errors.count, "error") %> prohibited this phrase from being saved:</h2>

      <ul>
      <% @phrase.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  Select the event type:
    <%= collection_select(:phrase, :event_type_id, EventType.all, :id, :name) %>
    Select the phrases to be used:
    <%= collection_select(:phrase, :template_pieces, TemplatePiece.all, :id, :name) %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我首先遇到批量分配问题,但我修复了将attr_accessible :template_pieces添加到短语模型的问题.我不确定这是否是修复它的正确方法,但至少它停止抱怨它无法大量分配受保护的属性.

I first had an issue with Mass Assignment, but I fixed that adding the attr_accessible :template_pieces to the phrases model. I'm unsure if that is the correct way of fixing it, but at least it stopped complaining that it could not mass assign the protected attribute.

现在,提交新短语时出现以下错误:

Now, I'm getting the following error when submitting a new phrase:

"1"的未定义方法"each":字符串

undefined method `each' for "1":String

我认为这是由于给定短语应该有许多template_pieces,但我目前只能一次提交一个.因此,它只是找到一个,尝试对其进行迭代,然后失败.

Which I kinda think happens due to the fact that there are supposed to be many template_pieces for a given phrase, but I'm currently only able to submit them one at a time. So it just finds the one, tries to iterate through it and fails.

我该如何解决?是否有更好的方法将带有has_many :through的模型输入数据库?我是否必须手动执行操作(如关闭默认控制器@phrase = Phrase.new(params[:phrase])?

How would I go about fixing that? Is there a better way of entering models with the has_many :through to the database? Do I have to do it manually (as in dismissing the default controller @phrase = Phrase.new(params[:phrase])?

谢谢!

推荐答案

您应该使用fields_for帮助器来包装嵌套的属性:

You should use the fields_for helper to wrap the nested attributes:

<%= f.fields_for :template_pieces do |template_f| %>
  <%= template_f.collection_select, :event_type_id, EventType.all, :id, :name %>
  Select the phrases to be used:
  <%= template_f.collection_select, :template_pieces, TemplatePiece.all, :id, :name %>
<% end %>

参考

fields_for 文档.

这篇关于Rails关联has_many:through的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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