I have a method like this
static getList (long colorid) {
ColorShades.findAll 'from ColorShades where color.id = :colorid', [colorid: colorid]
}
I'm using it like this:
def shadeIdsForAColor = ColorShades.getList(colorid as long)
Question
The method returns an ArrayList of ColorShade objects and each object has a shadeId with it.
What can I do to put those ids as a list of integers into the shadeIdsForAColor variable?
Doesn't this do it?
def shadeIdsForAColor = ColorShades.getList(colorid as long).shadeId
Related
def c = Service1.search(params)
String filename = 'FILE.csv'
def lines = c.collect {
String k = it[1]
[k[0..10], it[0]].join(',')
} as List<String>
each Item 'it' is an object like com.pakage1#123fgdg.. so it shows me an error that i can't get it[1], how i can get the value from this objects thank you .
it is an object . Suppose your class has a property named email.so can get value by using it.email. when you iterate over list it works like this .
Example java code is below if I have a class Movie
In java I will create its array by writing below code
Movie[] a = new Movie[4];
but how I can do it in rails So that when I check it on the console
#> a.type
#> "Movie"
Reather than
#> ActiveRecord::Relation
You just create a class by inherited from Array like below,
class MyArray < Array
#Add you custom methods
end
my_array = MyArray.new([1,2,3,4,5]) or
my_array = MyArray.new
my_array[0] = 1
my_array[1] = 2
That enough for basic customization of array data structure.
Literal translation would be
a = (1..4).map { Movie.new }
or (in Rails)
a = (1..4).map { Movie.create! }
but you normally shouldn't need to do it, since unlike in Java, you don't have limited-size arrays in Ruby.
Also, the type of that would then be Array (or rather its class; basic Ruby objects don't have type); the type of an element of that, a[0] for example, would be a Movie.
In the end, not quite sure what you're asking here...
When using projection on the properties, the result is returned as the list with the elements in the same sequence as that defined in the projections block. At the same time the property names are missing from the list and that is really disadvantageous to the developer as the result would be passed along and the caller needs to know what value belongs to which property. Is there a way to return a map from the Criteria query with property name as the key to the value?
so, the following code:
def c = Trade.createCriteria()
def remicTrades = c.list {
projections {
property('title', 'title')
property('author.name', 'author')
}
def now = new Date()
between('publishedDate', now-365, now)
}
This returns:
[['book1', 'author1']['book2', 'author2']]
Instead I would like it to return:
[[book:'book1', author:'author1'][book:'book2', author:'author2']]
I know I can arrange this way after getting the result but I earnestly feel that the property alias should have been used by the criteria to return a list of map that mimics the result of the SQL query and not a bland list.
Duplicate: Grails queries with criteria: how to get back a map with column?
And the corresponding answer (and solution): https://stackoverflow.com/a/16409512/1263227
Use resultTransformer.
import org.hibernate.criterion.CriteriaSpecification
Trade.withCriteria {
resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP)
projections {
property('title', 'title')
property('author.name', 'author')
}
def now = new Date()
between('publishedDate', now-365, now)
}
Agree with your question reasoning, this really should be part of the core GORM solution. That said, here's my workaround;
def props = ['name','phone']
def query = Person.where {}.projections {
props.each{
property(it)
}
}
def people = query.list().collect{ row->
def cols = [:]
row.eachWithIndex{colVal, ind->
cols[props[ind]] = colVal
}
cols
}
println people // shows [['name':'John','phone':'5551212'],['name':'Magdalena','phone':'5552423']]
If I take a number Object like so :
#objects = Object.all[1..5]
I no longer can perform a where method on #object.
Is there anyway, I can still perform..
#objects.where(:attribute => identity)
So long as I know all the objects are of the same class?
Once you triggered all an Array instance is returned, so answer to your question is no. There are some gotchas, though:
Keep a scope variable. I.e. if you need to use a scoped object in multiple places, do the following:
objects = Object.scoped
all_objects = objects.all
special_objects = objects.where(attribute: something_special).all
Continue playing with scoping:
objects = Object.skip(1).take(5)
all_objects = objects.all
special_objects = objects.where(attribute: something_special).all
Hacky and inefficient way:
all_objects = Object.all[1..5]
special_objects = object.select { |object| object.attribute == something_special }
I've got a domain class which I want to, at run time, do a database call to populate a list of objects as a property. I have a tree with a 'headMember' property and that property is an object which has the following function:
def marriages = {
def marriages = Marriage.findAll("from Marriage as m where m.mainMember.name=:name", [name:name])
return [marriages:marriages]
}
in my GSP, I use ${tree?.headMember?.marriages} to access the 'headMember' property of the model which is passed to the view 'tree' from the following function in the relevant controller:
def show = {
def tree = Tree.get(params.id)
render(view:'show', model:[tree:tree])
}
when I view this in my browser, i get:
Member$_closure1#3708ab98
where I'd expect a list.
Any ideas what I'm doing wrong?
Cheers.
When you call marriages, you are calling a closure and this closure is returned. I think that you should rework it to be a method, something like that:
static transients = ['marriages'] // tell hibernate that marriages is not a persistent property
List<Marriages> getMarriages(){
return Marriage.findAll("from Marriage as m where m.mainMember.name=:name", [name:name])
}
This way, when you call ${tree?.headMember?.marriages} in your GSP, the getMarriages() method is called and list of marriages should be returned.