How to return the first row using ActiveJDBC? - activejdbc

ActiveRecord has methods such as Entity.first, Entity.last, Entity.first.next etc (note there are no parameters passed).
Is there a similar construct in activejdbc?

There is "first": http://javalite.github.io/activejdbc/org/javalite/activejdbc/Model.html#first-java.lang.String-java.lang.Object...-
, but not others.
You can do this:
User.findAll().orderBy("age").limit(1); //--> first
User.findAll().orderBy("age desc").limit(1); //--> last
this way you control exactly is returned.

Related

dynamic multiple grouptext in jqgrid

I have implemented multiple grouping in jqgrid which is dynamic(can be on 1 or many columns). And I want the grouptext to have a checkbox, column name and the value.
$("#Grid").jqGrid({
...
grouping:true,
groupingView:{
...
grouptext:['<input type="checkbox" class="groupHeader"/> ColumnName: {0}']
})
This will give me grouptext in only one groupheader. But I can have grouping on several columns where the columnName is dynamic. I tried this which is not working:
var columnNames=['ABC','DEF','GHI'];
var grouptext1=['<input type="checkbox" class="groupHeader"/> columnNames: {0}']
$('#Grid').jqGrid('setGridParam',{grouptext:grouptext1})
I can change the grouptext1 according to my needs. But I need a way to bind it to the jqGrid.
How can that be done?
Note that you have a typo errr in your code. The JavaScript is case sensitive and you have write grouping text property not correct. It should be groupText instead of grouptext as you write.
If you can see, the groupingGroupBy method has a second parameter - options. That is exactley what you need. This parameter is a grouping options. Having this you can set the groupText. in a case you want like this
$("#grid").jqGrid('groupingGroupBy',
['col1','col2'],
{
groupText : [ 'checkbox for col1', 'checkbox for col2']
...
}
);

ActiveRecord outputs too much in select query for Rails

According to:
#actors = actors.to_sql
this query:
actors = Actor.select("actors.name")
supposed to be equal to:
SELECT actors.name FROM `actors`
And it works in my SQL; only selects actors.name ... so i try:
#actors = actors.to_json.to_s
and render on some view... I get this;
[{"name":"James Patrick Pe","id":null,"logo":{"url":"uploads/default.jpg","topbar_avatar":{"url":"uploads/topbar_avatar_default.jpg"}}},{"name":"SSS","id":null,"logo":{"url":"uploads/default.jpg","topbar_avatar":{"url":"uploads/topbar_avatar_default.jpg"}}},{"name":"Philhealth","id":nul ....
it's basically a JSON of everything... I only need one column for an AJAX reply. How can I do this with the ActiveRecord Way?
Do you use Paperclip? It's likely you have a method that appends something to your JSON.
What you need to do is to specify the fields you want to be exported.
#actors = actors.to_json(only: [:name]).to_s
Also please note that there is a better approach to to_json. You should definitely have serializers that take an object or collection in input and deal with the serialization.

Mixing 'Like' with comparators in an iif statement?

I'm attempting to use the query builder to formulate a query based on user input on a form, but I'm running into an issue.
I've been using this code to filter and check for null/"ALL" field before which is working fine.
Like IIf([Forms]![TransactionsForm]![ComboActStatus]="ALL","*",
[Forms]![TransactionsForm]![ComboActStatus])
But I run into an issue when I want to do the same thing with fields that signify a range. I attempted this:
IIf([forms]![TransactionsForm]![txtAmountFrom] Is Null Or
[forms]![TransactionsForm]![txtAmountTo] Is Null,
([dbo_customerQuery].[amount]) Like "*",
([dbo_customerQuery].[amount])>=[forms]! [TransactionsForm]![txtAmountFrom] And
([dbo_customerQuery].[amount])<=[Forms]![TransactionsForm]![txtAmountTo])
But it's causing my entire query to fail. How can I do this similar thing? Use "Like *" in the null case (return everything), but use comparators rather than "like" statements in the second case?
Unless I'm missing something LIKE "*" will return true for all values, so this should work:
IIf([forms]![TransactionsForm]![txtAmountFrom] Is Null Or
[forms]![TransactionsForm]![txtAmountTo] Is Null,
true,
([dbo_customerQuery].[amount])>=[forms]! [TransactionsForm]![txtAmountFrom] And
[dbo_customerQuery].[amount])<=[Forms]![TransactionsForm]![txtAmountTo])
)
The code that finally worked for me, and didn't have Access split it into separate lines was:
>=IIf([forms]![TransactionsForm]![txtAmountFrom] Is Null,0,[forms]![TransactionsForm]!
[txtAmountFrom]) And <=IIf([forms]![TransactionsForm]![txtAmountTo] Is Null,9999999999,
[forms]![TransactionsForm]![txtAmountTo])

Parametrized column for Ibatis Query works first time, fails on second call

I have a problem similar (or exactly the same) to what I found here:
How to map query for iBATIS with parameterized column in select clause?
But the answer given there seems to not work for me.
I have the following:
<typeAlias alias="resultado" type="java.lang.String"/>
<typeAlias alias="parametro" type="java.util.Map"/>
<select id="getValorVariable" resultClass="resultado" parameterClass="parametro">
SELECT $campo$ FROM $tabla$
WHERE $campoClave$ = #valorClave#
</select>
The first time it executes the select, it works. $campo$ has the value 'CIF'. The second time it runs, it should have the value 'FECHA' but it keeps 'CIF'. In the function calling the select, I check the map and its values, and it has the correct ones.
Any idea? Thanks for any help.
You're facing this problem because iBatis pre-compiles the entire query except for the param values. Those are dynamically added when the query runs. So, the first time your query gets run, it gets pre-compiled with the column names in your SELECT statement.
Add
remapResults="true"
to your <select> tag.
It should read like this:
<select id="getValorVariable" resultClass="resultado" parameterClass="parametro" remapResults="true">
Further, in your <sqlMapConfig> add this to the <settings> tag:
statementCachingEnabled="false"

Rails3 - Is there a way to do NOTLIKE?

I previously asked a question regarding pulling specific items out of a database if they contained a specific word in their string, someone kindly offered the following which did just the job:
def SomeModel < ActiveRecord::Base
scope :contains_city,
lambda { |city| where("some_models.address LIKE ?","%"+city+"%" ) }
end
However, I have some instances where I would like to do the opposite, i.e. pull out all the items which do not have the specified word in their string. Is there a way to do a NOT LIKE function? I have prevously seen people use '!=' for a NOT EQUALS, but have had no success along these lines for the LIKE function. Is there an equivalent or is it best to iterate through the database putting items in 2 separate databases based on whether they satisfy the LIKE condition?
You could try NOT LIKE in your query; MySQL supports this.
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html