Actually a bit more complicated then that unfortunately.
I have multiple API paths that need this functionality. However, I don't'
want to re-write code :p
*table1: (Accounts)*
- columns
- account_type_id
*table2: (AccountTypes)*
- primary_key: account_type_id
- value (Actual column name)
with those 2 tables/models, I need to (dynamically) be able to always pull
the value and display it along with the accounts data.
*so an example select that would be theoretically performed on a GET
request:*
select A.*, AT.value as sourceSystem
from Accounts A
join AccountTypes AT
on A.account_type_id = AT.account_type_id
*Example using SQL for POST (Create/Insert):*
account_type_id = select account_type_id from AccountTypes where value =
'uservalue'
insert into Accounts (columns) values (...., account_type_id )
In the POST example, I need to first figure out the account_type_id by
looking it up against the AccountTypes table.
I've got a generic/dynamic structure of code created (Worked on that
today), however I don't like it. I wanted to try to use dataset_module,
but it only solved half the problem.
*This is in every model class:*
# These are used to joint to another table, and pull out 1 value
# .association_join(:account_source_values)
# .select_append(:account_source_values__value___value)
def self.required_join_selects
{
account_source_values: :value
}
end
def self.required_post_parameters
{
source_system_value:
{
table: :account_source_values,
column: :value,
child_id: :account_source_id,
parent_id: :account_source_id
},
active: {},
account_ref: {},
enrollee_id: {}
}
end
def populate_display_hash_values(display_hash)
@source_system = display_hash[:source_system_value] if
display_hash.key?(:source_system_value)
end
def set_join_select_columns(table_name, column_name, value_to_set)
@source_system = value_to_set if column_name == :value && table_name ==
:account_source_values
end
*This is how I solved the GET part:*
return_object.params_extractor.join_conditions.each do |column_name, value|
join_details = model_object.allowed_join_conditions
filtered_results = model_object
.association_join(join_details[column_name])
.where(
"#{join_details[column_name].to_s}__#{column_name.to_s}": value)
end
filtered_results.each do |single_record|
model_object.required_join_selects.each do |table_name, column_name|
single_record.set_join_select_columns(table_name, column_name,
single_record[column_name])
end
end
*This is how I solved the POST part:*
# Check for required parameters
required_params = model_object.required_post_parameters
# Lookup foreign keys, and add them to the params_hash
display_hash_values = {}
# Lookup the account_source_id to use in creating a new record
required_params.each do |param, value_hash|
next if value_hash.empty?
key_value = settings.reader_db[value_hash[:table]]
.where(value_hash[:column] => params_hash[param])
.first
account_source_id = key_value[value_hash[:child_id]]
params_hash[value_hash[:parent_id]] = account_source_id
display_hash_values[param] = params_hash.delete(param)
end
reference = model_object.create(params_hash)
reference.populate_display_hash_values(display_hash_values)
On Tuesday, October 20, 2015 at 10:54:24 AM UTC-8, Jeremy Evans wrote:
>
> On Tuesday, October 20, 2015 at 11:10:43 AM UTC-7, Jeremy Swartwood wrote:
>>
>> I'm trying to create a dynamic/generic GET method for a REST api. My
>> code is working but I want to generalize it a bit.
>>
>> Essentially I don't want to have to create an instance variable
>> (source_system), and set it outside (ba.source_system = ba[:value]) in
>> an interation, just to use it inside (*sourceSystem: source_system**,*)
>> in a method. I want to just use "value" directly inside the model class.
>>
>> My model class joins to a lookup table of KEY/VALUE. I can access that
>> just fine with .each do. However, if I try to access the column in the
>> model, it doesn't exist as one of the columns.
>>
>> Models::Accounts
>> .where(return_object.params_extractor.where_conditions)
>> .qualify(:account)
>> .association_join(:account_source_values)
>> .select_append(:account_source_values__value___value)
>> .limit(page_opts[:per_page], page_opts[:offset])
>> .each do |ba|
>>
>> ba.source_system = ba[:value]
>>
>> puts ba.to_hash
>>
>>
>> I can access value when doing an iteration over it and calling ba[:value].
>>
>> but if I call a method on it inside that iteration:
>>
>> - ba.to_hash
>>
>> it doesn't have access to *value *in the to_hash method.
>>
>> calling p self.columns doesn't show the joined column t
>>
>> def to_hash
>> puts self.columns
>> {
>> id: account_id,
>> meta: create_meta,
>> accountNumber: account_ref,
>> * sourceSystem: value**,*
>> isActive: to_bool(active),
>> enrolleeId: enrollee_id,
>> _links: create_links
>> }
>> end
>>
>>
>>
> Assuming I'm reading this correctly, and you just want to use sourceSystem
> as the key in the hash, it seems easiest to just alias appropriately in
> your query:
>
> .select_append(:account_source_values__value___sourceSystem)
>
> Thanks,
> Jeremy
>
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.