Reputation: 15151
I have a Phrase
class that has_many
Phrase
as Translation
.
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
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".
<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?
class Translatability < ActiveRecord::Base
belongs_to :phrase
belongs_to :translation, :class_name => 'Phrase'
end
Upvotes: 5
Views: 135
Reputation:
I think you might be complicating things a bit with this method. If I understand correctly, you have Phrase
s that can be translated into multiple languages. It seems to me that what you really want to have is a Phrase
model that defines the language and has a reference to allow other translations to be related.
Phrase
would look something like this:
create_table :phrases do |t|
t.text :body
t.string :locale
t.integer :translation_ref_id
end
In the Phrase
model you can define translations
methods:
def translations
self.class
.where(translation_ref_id: translation_ref_id)
.where.not(id: id)
end
In your form, you would have multiple Phrases
instead of a single Phrase
with nested attributes. And in the controller, you would set the translation_ref_id
for each of the Phrase
s (you could use any unique identifier, incrementing integer ids are good; you don't need a separate model to keep track of ids).
Upvotes: 2