there is possible to related a related field ?
I did
1st object
_columns = {
'sale_price_unit': fields.related('sale_line_id','price_unit',string='Prix de vente',type='float', store=True, readonly=True),
}
the other object
_columns={
'sale_price_unit': fields.related('procurement_id','sale_price_unit',string='Prix de vente',type='float', store=True, readonly=True),
}
the value of the 2nd object alwayse equal 0.0 !!
try
_columns={
'sale_price_unit': fields.related('procurement_id','sale_line_id','price_unit',string='Prix de vente',type='float', store=True, readonly=True),
}
Related
I have a list of associado, and I want to select only the ones where eh_proprietario returns true. This is the mapping:
#possiveis_associados = associados.map { |e| e if e.eh_proprietario}
If I add a puts "#{e.eh_proprietario}" I can see it return true for two instances, but in my view, when I try to use this collection, I get an error because #possiveis_associados is nil.
<%= m.select :associado_id , options_from_collection_for_select(#possiveis_associados, :id, :razao_social), {include_blank: false}, {class: 'form-control'}%>
What am I doing wrong here?
You're looking for select, not map. Try
#possiveis_associados = associados.select { |e| e.eh_proprietario }
or, shorter
#possiveis_associados = associados.select(&:eh_proprietario)
My problem is that I need to fill an array with rails 4 to display top 3 results categorized by newspaper
Category of newspaper is: 0,1,2,3 or other, is random.
parsed_json: Is one array with values de newspapers, after news_source fill with news from newspapers.
SQL: http://i.stack.imgur.com/dP8BM.png
With this code only the last value of the array of a newspaper.
def index
if get_api_key
#parsed_json = Array.new
#news_source = Array.new
#Trae todos los datos asociados a la llave
#emission = Emission.where(key: get_api_key)
#Envío de datos a json
#emission.each do |p|
#weather_now = WeatherNowUy.where(city: p.state)
#weather_next_days = WeatherNextDaysUy.where(city: p.state)
#parsed_json << JSON.parse(p.news_source)
end
#parsed_json.each do |u|
#news_source = NewsUy.where(newspaper: u).limit(3)
end
end
end
Example of return, no show by newspaper 1 is also found in the array parsed_json.
Not
"News": [
{
"title": "Con un gol de Tevez, Boca venció a Godoy Cruz 2-0 y sigue en lo más alto",
"description": "El delantero marcó de penal en la victoria en La Bombonera. Meli habÃa abierto el marcador. Los \"Xeneizes\" comparten con San Lorenzo la primera posición.",
"newspaper": "0"
},
{
"title": "Barcelona picó en punta",
"description": "La primera fecha del fútbol español se terminará el lunes con el partido entre Granada y Eibar.",
"newspaper": "0"
},
{
"title": "Sampdoria, Chievo Verona y Fiorentina los lÃderes",
"description": "Este fin de semana se disputó la primera fecha de la serie A italiana.",
"newspaper": "0"
}
]
}
Change #news_source = NewsUy.where(newspaper: u).limit(3) to #news_source << NewsUy.where(newspaper: u).limit(3) in your last #parsed_json.each do loop. Because, if you use = operator, it will replace it every time with the next value and that's why you are getting only the last value. To get all the values, you need to accumulate those into the #news_source array using << operator.
So, you need this:
#parsed_json.each do |u|
#news_source << NewsUy.where(newspaper: u).limit(3)
end
I am currently fetching all notes in model by
#notes_list = current_user.notes.order(:title).group_by { |note| note.title[0] }
Later i added a column to notes table that is known a boolean column.
Now i want to fetch only true valued columns and false valued columns separately something like below
#notes_true_list = blah....
#notes_false_list = blah....
Can anyone help me?
Try the below code.
#notes_true_list = current_user.notes.where(known: true).order(:title).group_by { |note| note.title[0] }
#notes_false_list = current_user.notes.where(known: false).order(:title).group_by { |note| note.title[0] }
I'm using sfWidgetFormDoctrineChoice to build select elements in a form. This is how I'm using:
// Fill maquinaemisorid
$this->widgetSchema['maquinaemisorid'] = new sfWidgetFormDoctrineChoice(array('model' => 'SdrivingMaquina', 'add_empty' => 'Seleccione una Máquina', 'table_method' => 'fillChoice'));
// Fill idoperador
$this->widgetSchema['idoperador'] = new sfWidgetFormDoctrineChoice(array('model' => 'SdrivingOperador', 'add_empty' => 'Seleccione un Operador', 'table_method' => 'fillChoice'));
// Fill idturno
$this->widgetSchema['idturno'] = new sfWidgetFormDoctrineChoice(array('model' => 'SdrivingTurno', 'add_empty' => 'Seleccione un Turno', 'table_method' => 'fillChoice'));
For the first widget all is fine and values are taken from DB as should be but for the second one and the third is not working. As you may notice I'm calling a method fillChoice in all widgets table_method. This is the code for the three:
SdrivingMaquinaTable.class.php
public function fillChoice() {
$id_empresa = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
return Doctrine_Core::getTable('SdrivingMaquina')->createQuery('a')->where('a.idempresa = ?', $id_empresa)->execute();
}
SdrivingOperadorTable.class.php
public function fillChoice() {
$id_empresa = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
return Doctrine_Core::getTable('SdrivingOperador')->createQuery('a')->where('a.idempresa = ?', $id_empresa)->execute();
}
SdrivingTurno.class.php
public function fillChoice() {
$id_empresa = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
return Doctrine_Core::getTable('SdrivingTurno')->createQuery('a')->where('a.idempresa = ?', $id_empresa)->execute();
}
The method is the same always just changes the table used for each class. I check the generated queries and all are fine and if I run each on phpMyAdmin for example more than one value is fetched altough in idoperador and idturno just the latest is rendered. This is a very rare behavior and I can't find the error or mistake so I need some help or advice here.
As an adition I tried this in SdrivingRegistrosEmisoresForm.class.php (this is where the widgets are being generated):
$id_empresa = $this->current_user->getSfGuardUserProfile()->getIdempresa();
$choices_operador = Doctrine_Core::getTable('SdrivingOperador')->createQuery('a')->where('a.idempresa = ?', $id_empresa)->execute();
$this->widgetSchema['idoperador'] = new sfWidgetFormSelect(array('choices' => $choices_operador));
And this way works fine but the id for each item fetched isn't right meaning:
id item real_id
0 Item1 2
1 Item2 16
2 Item4 3
I talked about this in this topic but didn't get any answer, any help?
As stated in sfWidgetFormDoctrineChoice, if option *table_method* returns a collection like your methods, it renders choices with options *key_method* and method. By default , they are respectively getPrimaryKey() and *__toString()*.
Can we check you schema.yml and the __toString() ?
As for your last example the option choices of sfWidgetFormSelect should be an array an not a *Doctrine_Collection*
I would like to get the field names of a class and maybe store it in a list. Can anyone help? Thanks.
You can try this to get field names of domain class.
YourClass.declaredFields.each {
if (!it.synthetic) {
println it.name
}
}
You can use gormPersistentEntity for any domain object, this works with Grails 2.4.4 at least:
def names = Person.gormPersistentEntity.persistentPropertyNames
//returns ['firstName', 'lastName'...]
you can also get natural name using GrailsNameUtils like so:
def naturalNames = Person.gormPersistentEntity.persistentPropertyNames.collect {
grails.util.GrailsNameUtils.getNaturalName(it)
}
//returns ['First Name', 'Last Name'...]
def capitilizedNames = Person.gormPersistentEntity.persistentProperties.collect{
it.capitilizedName
}
//returns ['FirstName', 'LastName'...]
Just found it out, this one works:
def names = grailsApplication.getDomainClass('com.foo.Person').persistentProperties.collect { it.name }
You can iterate over the fields of a class like this.
YourClass.fields.each { println it.name }
If you need to put them into a list you could use collect() or populate it within the each.
http://groovy.codehaus.org/JN3535-Reflection