self-referential or other ways for same resource pair

31 views
Skip to first unread message

Paolo Wang

unread,
Sep 27, 2014, 9:10:40 PM9/27/14
to rubyonra...@googlegroups.com
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

Vivek Sampara

unread,
Sep 28, 2014, 1:33:22 AM9/28/14
to rubyonra...@googlegroups.com
Paolo, 

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. 

Language: 
has_many :language_translations


LanguageTranslation
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. 

Cheers




--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c214ccfe-bc9a-4e5a-85fe-003a325fa2f0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Paolo Wang

unread,
Sep 28, 2014, 10:05:02 PM9/28/14
to rubyonra...@googlegroups.com
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):

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

and LineItem controller:

def new
    @line_item = LineItem.new
    @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.

Regards

Vivek Sampara

unread,
Sep 28, 2014, 10:44:04 PM9/28/14
to rubyonra...@googlegroups.com
Do you have any issues with the form currently ?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Paolo Wang

unread,
Sep 28, 2014, 11:02:50 PM9/28/14
to rubyonra...@googlegroups.com
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.

Paolo Wang

unread,
Sep 29, 2014, 3:21:42 AM9/29/14
to rubyonra...@googlegroups.com
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 => source.id, :target_id => target.id)
        @order.line_items.create(:language_pair => pair)
      end
      create!
    end
  end
end

it work flawlessly.

Thank you all for the help.

Vivek Sampara

unread,
Sep 29, 2014, 6:04:19 AM9/29/14
to rubyonra...@googlegroups.com
Glad to see it working. Happy Coding :) 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages