This is kind of crazy and a little hard to explain.
Basically I have a table of customers and a table of metadata.
+-----------+ +-------------+
| Customers | | Metadata |
+-----------+ +-------------+
| id | | id |
| name | | customer_id |
+-----------+ | key |
| value |
+-------------+
- +-----------+ +-------------+
- | Customers | | Metadata |
- +-----------+ +-------------+
- | id | | id |
- | name | | customer_id |
- +-----------+ | key |
- | value |
- +-------------+
The data for the fields is supplied by an API that replies with json. Each field may contain more than one value, in which case they will be seperated by a comma.
Right now my code to break up the data looks a little like this:
customer.each do |key,value|
if key == CN_CONFIG['primary_key']
#This is unique to all customers.
if value == nil || value == ""
# You have no name!!!!!!!
break
end
if customers.include?(value)
# This is an update
update_c = Customer.find_by_name(value)
customers.delete(value)
else
# This is a create
update_c = nil
customer_name = value
end
end
if !ignore_fields.include?(key)
if list_fields.include?(key)
if value.scan(/,/).size > 0
# We have a list
puts "key: #{key} -- #{value} "
value.split(',').each do |v|
data_set.push({:key=>key,:value=>v.to_s.downcase.strip})
puts " value: #{v.to_s.downcase.strip}"
end
else
data_set.push({:key=>key,:value=>value.to_s.downcase.strip})
puts "key: #{key} | value: #{value.to_s.downcase.strip}"
end
else
data_set.push({:key=>key,:value=>value.to_s.downcase.strip})
puts "key: #{key} | value: #{value.to_s.downcase.strip}"
end
end
end
- customer.each do |key,value|
- if key == CN_CONFIG['primary_key']
- #This is unique to all customers.
- if value == nil || value == ""
- # You have no name!!!!!!!
- break
- end
- if customers.include?(value)
- # This is an update
- update_c = Customer.find_by_name(value)
- customers.delete(value)
- else
- # This is a create
- update_c = nil
- customer_name = value
- end
- end
- if !ignore_fields.include?(key)
- if list_fields.include?(key)
- if value.scan(/,/).size > 0
- # We have a list
- puts "key: #{key} -- #{value} "
- value.split(',').each do |v|
- data_set.push({:key=>key,:value=>v.to_s.downcase.strip})
- puts " value: #{v.to_s.downcase.strip}"
- end
- else
- data_set.push({:key=>key,:value=>value.to_s.downcase.strip})
- puts "key: #{key} | value: #{value.to_s.downcase.strip}"
- end
- else
- data_set.push({:key=>key,:value=>value.to_s.downcase.strip})
- puts "key: #{key} | value: #{value.to_s.downcase.strip}"
- end
- end
- end
Not really to important to the question though.
What question is, how do you filter upon a table of meta data?
Example say some of the keys listed in the metadata table are: gender, birth_year, siblings
Where:
gender =>
m or
fbirth_year => (any year YYYY)
siblings => (a name, and each customer can have infinite number)
(Btw this isnt the real data lol)
Now What I plan to do is group all the keys together to get all the keys that show up in the metadata table. These turn into field sets, all the values turn into check boxes that have a select menu to include or exclude the criteria.
Now, how do you write a query to return all the customers that meet the criteria?