Displaying records with static look up table source

Greetings All,

I hope you all understand what I am writing here. Lots of views involve
displaying date from static ‘look up’ tables: states, countries, type…

From all of the documentation I have read, the standard code conduct
@look_up_object = LookUpModel.find .where. conditions => ids match

I pursued that structure, and for long listings, so many calls to
really bothered me.

So I re-wrote the code on look-up for my case static item_conditions,
containing only two field: conditions and item_conditions_id:

In the view I now have, following found parent records:

    <% @icond = Itemconditions.find(:all, :readonly) %>
    <% @ih = Hash.new %>
    <% @ih2 = Hash.new %>
    <% @ih = @icond.to_a.map(&:serializable_hash) %>
    <% for kv in @ih %>
      <% @ih2[kv.fetch("item_conditions_id")]  = 

<% end %>

And then later for iterative record output I have in the view, ldr as
parent data source

<label class="item_listing">Condition:&nbsp;<%=

@ih2[ldr.item_condition_id.to_s] %>

Benchmarked this and vastly faster. Deployed it, no problems/errors. I
have researched Ruby and Ruby on Rails API. I have not found a simpler,
more native approach. Masters if you are aware of Ruby on Rails way,
please let me know.


Liz McGurty

displaying data

I would recommend you to remove this code in your view to a presenter
use AR include method to add a your relationships in the same query to
fetch from the database. It will avoid de n+1 issue.

2015-05-18 13:23 GMT-03:00 Elizabeth McGurty [email protected]:

parent data source




For more options, visit https://groups.google.com/d/optout.

http://about.me/thiagocifani http://del.icio.us/thiagocifani

Okay, Thiagocifani and others interested,

I have finally revised the structure of my application for plural table
names, which already existed, and singular model names. Across tables
have set up parent and foreign key with exactly the same field name and
data type. Within models I have defined belongs_to and has_many.

Now I am revisiting my views, in the case where the child value is a
value to the parent, the display is working fine. What I am finding
challenging is populating <select

The parent is advertiser, and the child is category…

Currently I have:

<label>Advertisement Category:</label>
<% @categories = Category.find(:all, :readonly => true, :order=>

“category_type”) %>
<% @cats = options_from_collection_for_select(@categories,
:category_id, :category_type) %>
<%= nf.select( :category_id, @cats) %>

How do I rewrite this? From the controller, how should I set up my data
source? Tried includes and Parent.child.find



Thank you, Thiagocifani! I was not aware of the include method. And,
the way, I visited your about.me page and it is very impressive.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs