self-referential or other ways for same resource pair

Hi to all.

I am working for a translation agency and I have the task to create an auto-estimate system and ordering/invoicing administration system. I am stuck on how to manage the language pairs. I created a Language model with the language name and a LanguagePair with a source language, a target language and a rate per words attributes. I tried with:

LanguagePair belongs_to :source, :class_name => “Language”, :foreign_key => “source_id” belongs_to :target, :class_name => “Language”, :foreign_key => “target_id”

and also by replacing the source and target in the LanguagePair with a string type and loading it with a Language.all.permutation(2).to_a and eliminate the need for nesting. In the end I will create a sort of cart and lineitem model and add the LanguagePair to the cart as a LineItem.

Is there a “best” way to do this maybe with a Language self-referential model?

thanks in advance


I think you’ve going in the right direction but i’d recommend a few changes based on my experience. Translation Agency has different rates for each language. and the rate per language for A → B Is not the same as B → A. So its better to maintain the cost details in Another model unique for one direction.


has_many :language_translations


belongs_to :language

belongs_to :target_language, :class_name => “Language”

#An float/integer field for cost.

Also i dont think you have to specify foreign_key condition unless your association name and foreignkeys are different…

eg blongs_to :car, :class_name => “Vehicle”, :foreign_key => “bus_id”

If you have car_id as the foreign key. then you can ignore mentioning it in the model.


Thank you very much.

Using ‘Language.all.permutation(2). to_a’ I’ve got all the combinations (A->B and B->A) and seeded the LanguagePair model. For the cost I was thinking to puts a decimal column in the LanguagePair model. At the moment I am working on the form(inside the lineitem views):

<%= label_tag :from, "Source Language" %>
<%= select_tag :from, options_for_select(@from_lang), {id: 'source_select'} %>
<%= label_tag :to, "Target Language" %>
<%= select_tag :to, options_for_select(@to_lang), {id: 'target_select'} %>

and LineItem controller:

def new @line_item = @from_lang = Product.uniq.pluck(:from) @to_lang = Product.where(from: @from_lang.first).pluck(:to) end

I haven’t got a lots of experience with Rails and I am always searching for a better ways to write things.


Do you have any issues with the form currently ?

It works fine. I have problem on the ActiveAdmin gem form(formtastic-based for form construction. In the ActiveAdmin/Order I have

form do |f| f.inputs do f.input :title f.has_many :line_items, :allow_destroy => true do |lp| lp.input :language_pair end end f.actions end

I would like to pass two variable :source and :target just as the normal form, but it seems I cannot do it with ActiveAdmin. I think I will go with a custom form.

I finally got it working in ActiveAdmin without a custom form. I’ll post how I did it for references if somebody need. First I set my custom attributes in the LineItem model:

class LineItem < ActiveRecord::Base attr_accessor :source attr_accessor :target

(it doesn’t work adding :source and :target to the ActiveAdmin file under permit_params, I get an error that there are no column with :source/:target if not added to the model with attr_accessor)

ActiveAdmin.register Order do

permit_params :id, :title, line_items_attributes: [:id, :order_id, :language_pair_id, :source, :target]

form do |f| f.inputs do f.input :title f.has_many :line_items, :allow_destroy => true do |cf| cf.input :source, :collection => Language.pluck(:lang) cf.input :target, :collection => Language.pluck(:lang) end end f.actions end

controller do def create @order = Order.create(:title => params[:order][:title]) params[:order][:line_items_attributes].each do |x, y| source = Language.find_by_lang(y[:source]) target = Language.find_by_lang(y[:target]) pair = LanguagePair.find_by(:source_id =>, :target_id => @order.line_items.create(:language_pair => pair) end create! end end end

it work flawlessly.

Thank you all for the help.

Glad to see it working. Happy Coding :slight_smile: