The problem seems trivial, but I can't find any reasonable solution. I have list of countries with translations stored in Globalize3 translation tables. How can I fetch the list of countries sorted by name?
Country name isn't stored directly in the model, but in separate table. Is there any reasonable way to sort the result other than manual sql query, or manualy sorting result table after AR query is complete?
Country.with_translations(I18n.locale).order('name') for current locale.
Edit:
You can also use fallbacks:
Country.with_translations(I18n.fallbacks[I18n.locale]).order('name')
Country.with_translations.order('name')
Related
I have this situation. Starting from a table, I have to check all the records that match a key. If records are found, I have to check another table using a key from the first table and so on, more on less on five levels. There is a way to do this in a recursive way, or I have to write all the code "by hand"? The language I am using is Visual Fox Pro. If this is is not possible, is it al least possible to use recursion to popolate a treeview?
You can set a relation between tables. For example:
USE table_1.dbf IN 0 SHARED
USE table_2.dbf IN 0 SHARED
SET ORDER TO TAG key_field OF table_2.cdx IN table_2
SET RELATION TO key_field INTO table_2 ADDITIVE IN table_1
First two commands open table_1 and table_2. Then you have to set the order/index of table_2. If you don't have an index for the key field then this will not work. The final command sets the relation between the two tables on the key field.
From here you can browse both tables and table_2's records will be filtered based on table_1's key field. Hope this helps.
If the tables have similar structure or you only need to look at a few fields, you could write a recursive routine that receives the name of the table, the key to check, and perhaps the fields you need to check as parameters. The tricky part, I guess, is knowing what to pass down to the next call.
I don't think I can offer any more advice without at least seeing some table structures.
Sorry for answering so late, but the problem was of course that the recursion wasn't a viable solution since I had to search inside multiple tables. So I resolved by doing a simple 2-Level search in the tables that I needed.
Thank you very much for the help, and sorry again for answering so late.
First time poster and a Freemarker novice. I'm hoping someone can assist on this. I am currently referencing data from a 1-to-many table using the <#data> directive. I want to sort the results in a particular field order, but I think that can only be done by the <#list> directive. Here's what I have so far:
There's a CART_ABANDONMENT table with the following fields:
CUSTOMER_ID_
PRODUCT_ID
PRODUCT_NAME
PRODUCT_PRICE
ABANDONED_DATE
<#data CART_ABANDONMENT as abandonment><br>
<#filter CUST_ID="${CONTACTS_LIST.CUSTOMER_ID_}"><br>
<#fields PRODUCT_ID PRODUCT_NAME PRODUCT_PRICE ABANDONED_DATE><br>
${abandonment.PRODUCT_NAME} ${abandonment.PRODUCT_PRICE}<br>
</#data>
What I want to do is to list all related results (by CUSTOMER_ID_) and sort them by PRODUCT_PRICE, descending.
It may be something simple, but I haven't been able to find the answer.
Any guidance would be appreciated.
Freemarker is powerful tool tool but it's just template engine.
It has the sort directive for list, but it can be applied only to built-in types: String, Number and Date.
You may convert list of you complex type to one or more lists of built-in types and use ?sort in template.
Another way is to pass already sorted(as you want) list to template before processing.
What you want sounds like something that should be solved on the database (SQL) level, especially where you say "list all related results (by CUSTOMER_ID_)". FreeMarker is only the V (for View) in MVC.
I'm trying to order search results using an equation that uses Products and User has inputs. In other words, I have an equation that uses data from both Products and current_user. I want to be able to order my search results by the number I get from the equation. I'm pretty lost on even were to begin with this. Has anyone done something similar or have any ideas of how to best handle the sort/order? From my understanding, "order" is usually used to sort actual columns in the SQL database and not a method.
I was originally thinking I would need to pass the current_user into the model. I know that's not best practice, but Sunspot allows you to create custom fields in the model and then sort on those fields. Unfortunately, this doesn't work since sunspot needs to index the fields it searches and sorts by.
I'm currently using Sunspot and would like to keep using it even if need to make modifications. I'm also using pagination.
You cannot calculate complex equations in SQL. You are restricted to simple calculations.
Possible Solutions:
Depending on how many results you get it might be possible to just
use the ruby sort method.
Calculate the result of your equation and store them in a new
column or table
Your problem could be associated with "matching".
I have a very large set of data on which I'm doing a great deal of post-query manipulation (sorting, filtering, etc etc). I would like do all this manipulation on an array of ActiveRecord objects that contains only the information necessary to the sorting, filtering, and paging, and then add the data necessary for display at the end.
For example, let's say I have a database with two tables: baseball_players and player_infos. The baseball_players table contains all of the interesting stuff (stats, team, name, birthday, etc etc etc). Player_infos contains player_id, player_rank, and player_position. I have 15000 players, and I want to find the numbers 100-150 of the best catchers of all times. I retrieve an array of all player_infos, filter to only catchers, sort by player_rank, and then retrieve records 100-150.
What is the best way to merge the resulting player_info records with their corresponding baseball_player records? Hash.merge would work perfectly, but I don't want to convert these objects to Hashes. Does ActiveRecord support something similar?
Note that I have a restriction where I cannot simply query the data using SQL - I have to manually sort and filter an object containing all 15000 player_info records.
I believe you are looking for ActiveRecord::Base#update.
I have an assignment where I have two tables. Both of these two tables have multiple records that can be grouped by a certain ID creating record sets within those two tables
Those record sets can have various number of records. The trick is I have to compare those two tables and compare them by those record sets. If one record set ordered by update date (one of the record fields) doesn't find an identical record set in another table, I have to output that record set
What is the best way to do it? How do I compare two different tables by record groups/record sets/record blocks?
Should I use sub-query factoring? Should I temporary tables? Should I use something else?
Thank you very much for your generous responses and please let me know if I made my question unclear
i guess you just need a minus query to show the differences.
If you use Toad there is a specific function. Or you can use the minus operator or read this other post link