Hola,
Tengo Traducciones y Oraciones. Los originales también son
traducciones, así que hay un campo que engancha el original si es una
traducción (subtranslations). El texto está en las Oraciones.
class Sentence < ActiveRecord::Base
has_many :translations
end
class Translation < ActiveRecord::Base
belongs_to :sentence
has_many :subtranslations, :class_name => 'Translation', :foreign_key =>
'translation_id'
end
Al cargar las traducciones, incluyendo las oraciones, quiero sólo las
oraciones que tengan un cierto texto.
@t = Translation.find(:all,:include =>
[:sentence,{:subtranslations=>[:sentence]}],:conditions=>['sentences_subtranslations.name
like ?','%cierto texto%'])
¡Pero no funciona!
Lo que sí funciona es hacer:
@t = Translation.find(:all,:include =>
[:sentence,{:subtranslations=>[:sentence]}],:conditions=>['sentences_translations.name
like ?','%cierto texto%'])
Es decir, usando 'sentences_translations' en lugar de
'sentences_subtranslations' que es el nombre real de la relación. No
está bien, ¿no? No sé si será un bug ...
Pongo abajo el controlador y la migración y el rhtml (los modelos están
arriba) por si alguien quiere probarlo. Es bien cortito. Probé
bastante, porque no encontraba en ningún lado cómo hacer referencia en
:conditions a campos de :include anidados. Ni siquiera estoy seguro de
que sea así, pero bueno, por lo menos ahora parece andar.
Eduardo.
class TranslatorController < ApplicationController
def index
s_es = Sentence.find_or_create_by_name('hola mundo')
s_en = Sentence.find_or_create_by_name('hello world')
t_es =
Translation.find_or_create_by_sentence_id_and_translation_id(s_es.id,nil)
t_en =
Translation.find_or_create_by_sentence_id_and_translation_id(s_en.id,t_es.id)
# ANDA
@t = Translation.find(:all,:include =>
[:sentence,{:subtranslations=>[:sentence]}],:conditions=>['sentences_translations.name
like ?','h%'])
# NO ANDAN
[EMAIL PROTECTED] = Translation.find(:all,:include =>
[:sentence,{:subtranslations=>[:sentence]}],:conditions=>['subtranslations.sentences.name
like ?','h%'])
[EMAIL PROTECTED] = Translation.find(:all,:include =>
[:sentence,{:subtranslations=>[:sentence]}],:conditions=>['subtranslations.sentence.name
like ?','h%'])
[EMAIL PROTECTED] = Translation.find(:all,:include =>
[:sentence,{:subtranslations=>[:sentence]}],:conditions=>['sentences_subtranslations.name
like ?','h%'])
end
end
# Migraciones
class CreateTranslations < ActiveRecord::Migration
def self.up
create_table :translations do |t|
t.column :name, :string
t.column :sentence_id, :integer
t.column :translation_id, :integer
end
end
def self.down
drop_table :translations
end
end
class CreateSentences < ActiveRecord::Migration
def self.up
create_table :sentences do |t|
t.column :name, :string
t.column :sentence_id, :integer
end
end
def self.down
drop_table :sentences
end
end
# index.rhtml de la vista (debería poner 1/hola mundo/hello world en
líneas separadas
<%= @t.size %>
<br>
<%= @t[0].sentence.name %>
<br>
<%= @t[0].subtranslations[0].sentence.name %>
_______________________________________________
Ruby mailing list
[email protected]
http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar