Create a double join query using Rails Admin - mysql

I am trying to create a form using Rails Admin, version 4.0.0. For this form I have tables called items, locations, cities and item_in_city. Tables are related in the following ways:
items table has a column called location which is array or location ids where item is present
location table has a column called city_id which connects it to city table
there is item_in_city table which has item_id and its corresponding list of city_id
Now, I want to create a form using Rails Admin, where I can give option to add a new item. While adding this new item I want to give an option to select (multiple) cities and corresponding to cities I want to give list of locations which can be selected.

I figured out the solution, So I thought of posting it as it can be useful to others. Combination of city name and location can be posted by defining an enum method in the following way
def location_enum
Location.all.collect {|l| [ l.city.name + ' ' + l.name, l.id] }.sort
end

Related

Joining 2 tables together and using the where function based on a separate mysql query

I am building a training platform for work. I have created the requirements for a user to be trained based on a role given to them. If that role is aligned to a document it will sit against the user. I have managed to get most of the way but am struglling on the best way to finish the where statement within mysqli.
tbldocfiles is a list of my files. I am looking at docid (could be multiple files associated to the document)
tbltrainingaccess sets the roles (driver, warehouseman, customer services) and shows which role (by id) is associated to the document in docfiles.
tblusertraining is the list of users and what role they have associated to them. (driver, warehouseman, customer services).
I am listing the documents associated to the user so have thought the following is the best way:
Look at the user and how many roles he/she is allocated
Look at the roles returned in point 1 (where function)
Identify and match the documents that have the same roles as the user (Join function)
create the list, then look at the unique values for docid. (distinct value)
Example User Bri has the driver and warehouseman role.
There are 5 documents in the db, 3 of them are associated to the driver role (docid 1,2,3) and 2 of them are associated to the warehouseman role (docid 2,4) the 5th document is associayted to customerservice.
My query should do this:
List all documents associated to the roles, that are associated to the user Bri
1
2
3
2
4
Now select unique values (using docid) from the above list:
1,2,3,4.
So my answer will be a used as a count function at the end using mysql_fetch_rows
SELECT DISTINCT tbldocfiles.docid FROM tbldocfiles LEFT JOIN tbltrainingaccess ON (tbldocfiles.docid = tbltrainingaccess.docid) where groupid='1' or groupid='9'
The above code works. but i've got myself confused.
The where statement needs to be the result of a query similar to :
select * from tblusertrainingrole where userid='1' (1 will be a variable based on page selection)
the result in this would be 1, 9 which are the groupid results.
Basically any help would be appreciated! I am sure it will be simple but have burnt myself out on this for a while and most answers in here helped with joining but not the where statement (that I could find)
Thank you in advance everyone!
You can do a select statement in the where. Since it is an or statement you can use in for the results. Please replace * with the column name for the value you need. Should look like
where groupid in (select * from tblusertrainingrole where userid = '1')

Rails, MySql, JSON column which stores array of UUIDs - Need to do exact match

I have a model called lists, which has a column called item_ids. item_ids is a JSON column (MySQL) and the column contains array of UUIDs, each referring to one item.
Now when someone creates a new list, I need to search whether there is an existing list with same set of UUIDs, and I want to do this search using query itself for faster response. Also use ActiveRecord querying as much as possible.
How do i achieve this?
item_ids = ["11E85378-CFE8-39F8-89DC-7086913CFD4B", "11E85354-304C-0664-9E81-0A281BE2CA42"]
v = List.new(item_ids: item_ids)
v.save!
Now, how do I check whether a list exists which has item ids exactly matches with that mentioned in query ? Following wont work.
list_count = List.where(item_ids: item_ids).count
Edit 1
List.where("JSON_CONTAINS(item_ids, ?) ", item_ids.to_json).count
This statement works, but it counts even if only one of the item matches. Looking for exact number of items.
Edit 2
List.where("JSON_CONTAINS( item_ids, ?) and JSON_LENGTH(item_ids) = ?", item_ids.to_json, item_ids.size).count
Looks like this is working
You can implement a has many relation between lists and items and then access like this.
List.includes(:item).where('items.id in (?)',item_ids)
To implement has_many relation:
http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

Storing inputs in mysql array

I got a text field for entering the new categories and submit with ajax ..
My script is working but everytime i submit new category mysql is creating one row to hold single category name .
I would like to create something like this eachtime when i add a new category the same single row should get updated
Id. User id. Category
1. 23. Personal , shopping , others +
So everytime i update a category name the row should be updated with the comma separated value . And also i would like to fetch the result in a select box

BO XI 3.1 List of values prompt item description return item key

I'm trying to define a LOV with Universe Designer that when used in a #Prompt shows the user the item description, but gives back the corresponding item key as result of user selection.
e.g. From a dimension table of countries with two columns as COUNTRY_ISO and COUNTRY_NAME, I would like to show COUNTRY_NAME values to user with #prompt, but get back the corresponding COUNTRY_ISO as return value of #prompt.
This is possible using Index Awareness. I'll describe the solution using the sample "Island Resorts Marketing" universe, but the concept should map to your specific need.
For this example, we'll create a prompt on the "Country" object in the "Resort" class. The prompt will display the country names, but the condition will apply to the country_id field.
First, we need to identify the keys. On the Keys tab of the Resort\Country object, add a new Primary Key using Resort_Country.country_id as the source:
(in your case, you'll do this on the COUNTRY_NAME object, using COUNTRY_ISO as the primary key)
Next, we create the predefined condition that will include the prompt. Create a new Predefined Condition, named "Select country", with the following definition:
Resort_Country.country_id = #Prompt('Choose a country','A','Resort\Country',Mono,primary_key)
What we're doing is is applying the condition to the ID field (resort_country.country_id), but using the country name (the Country object) as the source. Note the "primary_key" parameter -- this tells BO to use the PK from the referenced object.
Now to test. Create a WebI report and select the "Select country" filter along with a field to display. I chose Service Line:
I run the report and am presented with a list of country names. I choose France, and I get a list of the Service Lines associated with France. The SQL generated by this query, reflecting my prompt selection, is:
SELECT
Service_Line.service_line
FROM
Service_Line,
Country Resort_Country,
Resort
WHERE
( Resort_Country.country_id=Resort.country_id )
AND ( Resort.resort_id=Service_Line.resort_id )
AND ( Resort_Country.country_id = 2 )
Note the very last line, which shows that the condition is applied using the ID, even though I selected the country name "France".
References:
- XI3.1 Designer Guide #Prompt info, starting on page 520
- Dave's blog on Index Awareness
- Dave's blog on Index Awareness with prompts

What is the best way to merge 2 tables with Active Record and Mysql

We need to allow users to customize their entities like products... so my intention was to have a product table and a custom_product table with just the information the users are allowed to change.
When a client goes to the product I want to merge the information, means I want to merge the two tables - the custom overwrites the default Products table.
I know that in mysql there exists a ifnull(a.title, b.title) way but I was wondering if there is any nice and efficient way to solve this in Rails 4 with Active Record. Assume that the products and custom products table have just 2 columns, ID and TITLE
I think you can convert both objects to JSON and then handle their params as a hash, using the merge method:
class Product
end
class Customization
belongs_to :product
end
a = Product.find(...)
b = a.customization
c = JSON(a.to_json).merge(JSON(b.to_json).reject!{|k,v| v.nil?})
Therefore c will contain all params from Product eventually overridden by those in Customization which are not nil.
If you still want to use a Product object with hybrid values (taken from Customization) you can try this:
a.attributes = a.attributes.merge(b.attributes.reject!{|k,v| v.nil?})
In this case a will still be a Product instance. I would recommend to keep the same attributes in both models when doing this.