I have a Phrase
class that has_many
Phrase
as Translation
.
app/models/phrase.rb
class Phrase < ActiveRecord::Base
has_many :translatabilities
has_many :translations, through: :translatabilities
has_many :inverse_translatabilities, class_name: "Translatability", foreign_key: "translation_id"
has_many :inverse_translations, through: :inverse_translatabilities, source: :phrase
accepts_nested_attributes_for :translatabilities
end
app/models/phrases_controller.rb
class PhrasesController < ApplicationController
def index
@phrases = Phrase.all.page params[:page]
@translations = @phrases.count.times.map do |i|
translation = Phrase.new
translation.translatabilities.build
translation
end
end
end
And I want to add "translatability" forms for each "phrase".
app/views/phrases/index.html.erb
<table>
<tbody>
<% @phrases.each do |phrase| %>
<tr>
<td><%= phrase.text %></td>
</tr>
<tr>
<td>
<%= form_for @translations do |f| %>
<%= f.text_field :text %>
<%= f.submit 'translate' %>
<%= f.fields_for :translatabilities do |t_form| %>
<%= t_form.hidden_field :id %>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
This code has infinite loop bug.
undefined method `phrase_phrase_phrase_phrase_phrase_phrase_...
How can I create forms for translation of original phrases?
app/models/translatability.rb
class Translatability < ActiveRecord::Base
belongs_to :phrase
belongs_to :translation, :class_name => 'Phrase'
end