Make a value appear first in drop down others after - postgresql

I have a collection of Industries that I am displaying in a drop down using this code:
<%= ff.select :area_of_business_id, Industry.all.map { |x| [x.name, x.id] }, {include_blank: "Select an industry"}, class: 'droplist default-droplist required' %>
I would like one of the specific Industries from the list to appear first in the dropdown and the rest to appear after that in alphabetical order. For example, if I have this list:
Airlines
Broadcasting
Chemicals
Entertainment
Insurance
And, I would like Entertainment to appear first, then I want the order to be changed to this:
Entertainment
Airlines
Broadcasting
Chemicals
Insurance
What is the cleanest way to do this?

Yes possible as below. Say you have a instance variable inside the controller:
#option_values = Industry.order("name <> 'Entertainment', name asc")
.pluck(:name, :id)
Now do,
<%= ff.select :area_of_business_id, #option_values, {include_blank: "Select an industry"}, class: 'droplist default-droplist required' %>
That part order("name <> 'Entertainment', name asc") will put the record with name = 'Entertainment' first, then the rest in decreasing order.

Rails support priority of options out of the box for time_zones only. In all other cases you should do it manually use something like this:
<% industries = [Industry.find_by(name: 'Entertainment')] + [Industry.where.not(name: 'Entertainment')] %>
<%= ff.select :area_of_business_id, industries.map { |x| [x.name, x.id] }, {include_blank: "Select an industry"}, class: 'droplist default-droplist required' %>
but I recommend to add additional filed to Industry model for specify position in lists and sort by this filed.

Related

How can I add custom attribute to MUI DataGrid Column Header?

I need to add data-test attribute to column header for automated testing of different operations, such as sorting, filtering etc., so that scripts could find where to click.
How it can be done? I would like to keep all the functionality of default headers, just add the attribute.
Thanks!
You can use the renderHeader and pass the attributes you want like below:
columns={[
{
field: "name",
renderHeader: (params) => (
<span data-testid="header-test-id"> // Here pass your data-testid
{"Header with attr "}
</span>
)
}
]}

Rails, nested has_many association, finding sum of field of last children of each

I have nested models like (using PostgresSQL)
class User
has_many :pages
end
class Page
has_many :details
belongs_to :user
end
class Detail
belongs_to :page
#It has a column named "count"
end
What I need is say
User with id=1, it has many pages with ids= 1,2,3.
these pages with ids(1,2,3) has many details.
What I need is to get latest detail of each of these pages and get sum of field named "counts" from these latest details
What I 'm doing now is
page_ids = current_user.pages.pluck(:id)
last_followers = Detail.select("DISTINCT ON(page_id) *").where(page_id: page_ids).order("page_id, created_at DESC")
last_followers.each { |detail| last_sum += detail.counts }
But it doesn't look ok to find page_ids, than details and than looping through all of them.
Is there a direct way like single query, thanks
Not entirely sure of what you ask for but here's a try:
sum = current_user.pages.map { |page| page.details.last.counts }.sum

Zend_Form display forms from DB table rows

I'd like to build a dynamic FORM according to DB Table records.
It'a room reservation module.
Hotel has several room types with descriptions.
When booking a hotel user should see form like:
-- firstname [text-input]
-- lastname [text-input]
-- check-in [text-input and datepicker]
-- check-out [text-input and datepicker]
-- Room1 Title:Room Description [Text-input form for number of rooms]
-- Room2 Title:Room Description [Text-input form for number of rooms]
-- Room3 Title:Room Description [Text-input form for number of rooms]
-- Room4 Title:Room Description [Text-input form for number of rooms]
I have this Room records in a table.
How can I build this with ZEND_FORM?
I think it should be smth like foreach of objects of rooms
I also would like to add auto calculations.
Each room has specific price per night.
I want to sum-up number of rooms and multiply by number of nights.
So how can I accomplish it using Zend_Form?
Should I add some helpers? new type of text element?
If yes, please provide some sources to guide's and How To's with examples.
Thank you in advance.
I had something similar but with checkboxes, this is an adaptation, I hope it helps.
class Form_Booking extends Zend_Form {
function init()
{
$this->addElement('text', 'first_name', array (
'label' => 'First name',
'required' => true
));
// ... all the other fields
$count = 1000; // I used this value because I had a strange bug
//$rooms = array() the entries from your table
foreach ($rooms as $one)
{
$this->addElement('checkbox', "$count", array (
'label' => $one['room_title'],
'description' => $one['room_description'],
'belongsTo' => 'rooms',
));
$count++;
}
}
}

Grails Searchable Plugin for multiple domain classes and multiple search fields with a single submit button

I have multiple domain objects and having one to many, many to many relationships and search data comes from couple of tables and it is always same. I implemented Searchable plugin in my app and able to retrieve results when I have single search field like this:
<g:form url='[controller: "searchable", action: "searchContact"]' id="searchableForm" name="searchableForm" method="get">
<g:textField name="query" value="${params.query}" size="40"/>
<input type="submit" value="Search Contact" />
</g:form>.
But I have multiple text fields, check boxes and g:select boxes to get searchTerm. Based on any one of fields or multiple search selections I have to get search results. How to include all search fields in between and having a single submit button for all the params. Here is my search action code:
def searchContact = {
if (!params.query) {
return [:]
}
try {
String searchTerm = params.query
println searchTerm
return [searchResult: searchableService.search(searchTerm, params)]
} catch (SearchEngineQueryParseException ex) {
return [parseException: true]
}
}
Quick suggestions are appreciated.
You can pass all the terms in one String query separating each token/word by a space, so for example if you have two Domain classes one called Person and another one called Job and you search for "John" and "Engineer", your String query should be "John Engineer" and that should get you both domain objects.
Is that kind of what you are looking for?

what is the best way to use Html.DropDownListFor when you have a list of objects?

In my view model if have a:
List<Car>
where car has an Id and a name. I want to create a dropdown box using
Html.DropDownListFor()
what is the best way to hook this up as i want to have the value of the item be the Id and the display to be the Name of the Car.
What's in your view model,
to display the list you want you would use
<%= Html.DropDownList("DropDownName", new SelectList(yourListOfCar, "Id", "Name"))%>
so if you want to use DropDownListFor, you would use is like this
<%= Html.DropDownList(model => model.IdCar, new SelectList(yourListOfCar, "Id", "UserName"))%>
where model is your view model