Chosen field in active_admin - ruby

I have model User. One field in this model is gender. This field is integer. If user choose male I set 1, and if user chose female - I save this field as 0.
Also I have another models with the same problem in admin interface. For example in one of them I save day of week. It's very bad create some table with rows which will not change in future, but I doesn't have better solution
How I can make this better in active admin?

Get easy solution
column "Gender" do |user|
link_to (user.gender == 1) ? "Male" : "Female", "javascript:void(0)"
end

Related

Can I access a specific field value from an object through accessing by the related name, in Django?

I'm making a vote app (Django rest framework), where users can post content and have it voted on through two options. The Vote modelVote model code has a ForeignKey field to the Post model, with the related name attribute "votes". The vote model also has a field named 'option', where choices 'option1' and 'option2' can be selected.
My goal is to count the total number of votes on a post, number of option1 votes and number of option2 votes.
I've been able to count the number of votes on a post through:
queryset = Post.objects.annotate(
votes_count=Count('votes', distinct=True),
)
Post views
I am unsure how to count the number of option1 and option2 votes, separately.

Validation list with multiple criterias

I sort out my problem but I am wondering if an easier way doing it doesn't exist.
With the function onEdit(), I am creating from a first validation list, a second validation list in the next cell.
So, the code below is just for information.
var validationRange = data.getRange(3, myIndex, data.getLastRow());
var validationRule = SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).build();
activeCell.offset(0, 1).setDataValidation(validationRule);
In this validation list, I need to select a name, taking into account two additionnal criterias :
Name
Times
LastWeek
Patrick
2
W22
Rachel
5
W15
Claire
3
W14
Olivier
4
W16
Hélene
2
W20
It means that "Patrick" attended "2" times and last time was in week "W22".
I need to take into account these two criterias to select one of the attendee
I.E. : I try to let each person participate the same number of times but not every week (the oldest first)
So, I created a validation list with a "sorted key" that allows me to first see the person who has attended less and for longer.
Key Sorted
#02W20#Hélene
#02W22#Patrick
#03W14#Claire
#04W16#Olivier
#05W15#Rachel
This validation list is used 3, 5, 7 times in the same sheet because person can do different activities. Then, when all person are selected another script removes the values between # to keep only the name in the final sheet.
So, the question is :
could we create a validation list with multiple columns, the selected value being only the value of the first column.
I guess many users would enjoy it when we glance at questions about multiple criteras selection lists.
Thanks

Laravel hasOne Relation 2 columns

I have 2 Models first one is(Plan) and the second is (PlanPrice)
I have this columns in table plan_price:
$table->float('price')->comment('Price for one month or one year depends
on country code');
$table->string('country_code')->default('EG');
What I want is to get a plan with price depends on the user country code.
I think you should do this:
return $this->hasOne(PlanPrice::class, 'plan_id')->where('country_code',
$what_ever_your_code_is);

How to filter measure names in Tableau?

I was curious if anyone knew how to filter measure names so when a value is selected, only certain measure names appear. For example:
When 'Finance' is selected,
Show Profit, Sales, Revenue
When 'Investment' is selected
Show Stock Price, # of shares
Quick example of what I'm trying to accomplish. I know how to do this if you want to change one measure name, but not sure how to do it with multiple measure names. Any advice would be greatly appreciated, thanks!
You could create calculated fields(Analysis->Calculated Field) for each measure which feature IF statements. Like:
Calculate Field 1
IF [Type of Dimension] = 'Finance' THEN [Profit] ELSE NULL END
Calculate Field 2
IF [Type of Dimension] = 'Finance' THEN [Revenue] ELSE NULL END
Calculate Field 3
IF [Type of Dimension] = 'Investment' THEN [No of Shares] ELSE NULL END
Then drag all these calculated fields into your row shelf and your 'Type of Dimension' into your column shelf.
Right click on your dimension and select filter, then selecting your needed dimension should leave only the measures you wanted.
There's probably a smarter way but this is one way to do it, be it not very elegant.
See this for further info:
https://community.tableau.com/thread/118957

Play Framework: How to render a table structure from plain SQL table

I would be happy to get a good way to get the "table" structure from a plain SQL table.
In my specific case, I need to render JSON structure used by Google Visualization API "datatable" object:
http://code.google.com/apis/chart/interactive/docs/reference.html#DataTable
However, having an example in HTML would help either.
My "source" is a plain SQL table of "DailySales": its columns are "Day" (date), "Product" and "DailySaleTotal" (daily sale for that product). Please recall that my "model" reflects the 3-column table above.
The table columns should be "products" (suppose we have very small number of such). Each row should represent a specific date, and the row data are the actual sales for that day.
Date Product1 Product2 Product3
01/01/2012 30 50 60
01/02/2012 35 3 15
I was trying to use nested #{list} tags in a template, but unfortunately I failed to find a natural way to provide a template with a "list" to represent the "row data".
Of course, I can build a "helper object" in Java that will build a list of the "sales data" items per date - but this looks very weird to me.
I would be thankful to anyone who can provide an elegant solution.
Max
When you load your model order it by date and product name. Then in your controller build a map with date as index and list of model objects that have the same date as value of the map
Then in your template you have a first list iteration on map keys for the rows and a second list iteration on the list value for the columns.
Something like
[
#{list modelMap.keys, as: 'date'}
[${date},#{list modelMap.get(date), as: 'product'}${product.dailySaleTotal}#{ifnot product_isLast},#{/ifnot}#{/list}]#{ifnot date_isLast},#{/ifnot}
#{/list}
]
you can then adapt your json rendering to the exact structure you want to have. Here it is an array of arrays.
Instead of generating the JSON yourself, like Seb suggested, you can generate it:
private static Result queryToJsonResult(String sql) {
SqlQuery sqlQuery = Ebean.createSqlQuery(sql);
return ok(Json.toJson(sqlQuery.findList()));
}

Resources