Rails - Create form fields dynamically and save them - ruby-on-rails

I'm building an ad-system where users can dynamically create 'fields' for each ad type.
My models and example values:
AdType:
| id | name
|----|-----
| 1 | Hotel
| 2 | Apartment
AdOption:
| id | ad_type_id | name
|----|------------|-----
| 1 | 1 | Star rating
| 2 | 1 | Breakfast included?
| 3 | 2 | Number of rooms
AdValue: (Example after saving)
| id | ad_id | ad_option_id | value
|----|-------|---------------|------
| 1 | 1 | 1 (stars) | 5
| 2 | 1 | 2 (breakfast) | true
Ad: (Example after saving)
| id | description | etc....
|----|-----------------|--------
| 1 | very nice hotel | .......
So let's say I want to create a new ad, and I choose Hotel as the ad type.
Then I need my view to dynamically create fields like this: (I'm guessing?)
[Label] Star rating:
[hidden_field :ad_id] [hidden_field :ad_option_id] [text_field :value]
[Label] Breakfast included?
[hidden_field :ad_id] [hidden_field :ad_option_id] [text_field :value]
And also, how to save the values when the ad record is saved
I hope this is understandable. If not just ask and I'll try to clarify.

Start with the initial field in your form (AdType), then use javascript event listeners to check which option has been selected, and populate the field with the appropriate html.
jQuery probably has plugins that would do this for you, but coding it in plain JavaScript shouldn't be too difficult.

Related

Rails: create unique auto-incremental id based on sibling records

I have three models in my rails project, namely User, Game, Match
user can create many matches on each game
so table structure for matches is like
table name: game_matches
+----+---------+---------+-------------+------------+
| id | user_id | game_id | match_type | match_name |
+----+---------+---------+-------------+------------+
| 1 | 1 | 1 | practice | |
| 2 | 3 | 2 | challenge | |
| 3 | 1 | 1 | practice | |
| 4 | 3 | 2 | challenge | |
| 5 | 1 | 1 | challenge | |
| 6 | 3 | 2 | practice | |
+----+---------+---------+-------------+------------+
i want to generate match_name based on user_id, game_id and match_type values
for example match_name should be create like below
+----+---------+---------+-------------+-------------+
| id | user_id | game_id | match_type | match_name |
+----+---------+---------+-------------+-------------+
| 1 | 1 | 1 | practice | Practice 1 |
| 2 | 3 | 2 | challenge | Challenge 1 |
| 3 | 1 | 1 | practice | Practice 2 |
| 4 | 3 | 2 | challenge | Challenge 2 |
| 5 | 1 | 1 | challenge | Challenge 1 |
| 6 | 3 | 2 | practice | Practice 1 |
+----+---------+---------+-------------+-------------+
How can i achieve this auto incremental value in my rails model during new record creation.
Any help suggestions appreciated.
Thanks in advance.
I see two ways you can solve this:
DB: trigger
Rails: callback
Trigger (assuming Postgres):
DROP TRIGGER IF EXISTS trigger_add_match_name ON customers;
DROP FUNCTION IF EXISTS function_add_match_name();
CREATE FUNCTION function_add_match_name()
RETURNS trigger AS $$
BEGIN
NEW.match_name := (
SELECT
CONCAT(game_matches.match_type, ' ', COALESCE(count(*), 0))
FROM game_matches
WHERE game_matches.user_id = NEW.user_id AND game_matches.match_type = NEW.match_type
);
RETURN NEW;
END
$$ LANGUAGE 'plpgsql';
CREATE TRIGGER trigger_add_match_name
BEFORE INSERT ON game_matches
FOR EACH ROW
EXECUTE PROCEDURE function_add_match_name();
Please note that this is not tested.
Rails
class GameMatch
before_create :assign_match_name
private
def assign_match_name
number = GameMatch.where(user_id: user_id, match_type: match_type).count || 0
name = "#{match_type} #{number + 1}"
self.match_name = name
end
end
Again, untested.
I'd prefer the trigger solution since callbacks can be skipped or ommited altogether when inserting via pure SQL.
Also I'd add "match_number" column instead of the full name and then construct the name within the Model or a Decorator or a view Helper (more flexible, I18n) but the logic behind stays the same.
You should retrieve the last match_name for these user and game, split it, increase the counter and join back with a space. Unfortunately, SQL does not provide SPLIT function, so somewhat like below would be a good start:
SELECT match_name
FROM match_name
WHERE user_id = 3
AND game_id = 2
ORDER BY id DESC
LIMIT 1
I would actually better create a match_number column of type INT to keep the number by type and produce a name by concatenation the type with this number.

BDD how can i write a table if I have more than one variable in Given conditions

Below id my scenario that I am trying to automate:
Scenario Outline: create an invoice selecting
Given following <payment_term> is selected
And following <delivery_terms> is selected
And following <verzenderijnr> is selected
Examples:
| payment_term | delivery_terms | verzenderijnr |
| 1 | 1 | 1 |
| 2 | 2 | 2 |
When i transition the document to "final_invoice"
Then i expect the following transaction in my administration:
Examples:
| journal.id | account.id | document_date | due_date |
| VRK1 | 10016 | "2018-12-17" | 2019-01-24 |
You should use example with "scenario outline" and not only with scenario.
also, Example table should come at end after all "Given When Then" statement.
I'm not 100% sure what you're trying to accomplish exactly, but wouldn't your scenario be more easier to read if done like this?
Scenario Outline: Determine due date for sales invoices
Given I am creating a sales invoice on <Invoice date>
When I should <Pay within days>
Then the <Due date> should be correct
Examples:
| Invoice date | Pay within days | Due date |
| 2018-12-18 | 5 | 2018-12-23 |
| 2018-12-29 | 5 | 2019-01-02 |

Understanding Slowly Changing Dimension Type 2

I am having difficult time understanding how to use slowly changing dimension type 2, in my scenario.
I have gone through different tutorial websites but they don't fit.
I have an employee dimension table containing:
+-----+---------------+------------+------------+
| id | employee | designation| Location |
+-----+---------------+------------+------------+
| 1 | Ola | CEO | Newyork |
| 2 | Ahmed | DEVELOPER | California |
| 3 | Ola | Manager | California |
+----------+----------+------------+------------+
I have a Account Fact table
+-------+----------+
|emp_id | Amount |
+-------+-----------
| 1 | 2000000 |
| 2 | 300000 |
+----------+-------+
Now we see that the dimension has changed, and thus a new ID to same Ola employee has been given.
How would we manage in the fact table?
The new ID of Ola will not be found in Fact Table.
so if we add a new row in fact, with new ID of Ola, how would we link that they are same employee, when they are identified differently, 'primary key'.
How would we distinguish this employee is not a new employee and actually location / designation has been changed.
I am sure there are many ways of doing it, here's one way - Have an "employee_Key" in your dimension Table which is unique for an employee. So your dimension table will look like this -
id | emp_key | employee | designation| Location |Valid From| Valid To |
-----|---------|------------|------------|------------|----------|----------|
1 | EMP1 | Ola | CEO | Newyork |1/1/1900 |1/1/2016 |
2 | EMP2 | Ahmed | DEVELOPER | California |1/1/1900 |NULL |
3 | EMP1 | Ola | Manager | California |1/2/2016 |NULL |
You can continue loading your fact table with the "New" ID for the employee. In this case you will have 2 different sets of Keys for that employee.
+-------+----------+
|emp_id | Amount |
| 1 | 2000000 |
| 2 | 300000 |
| 3 | 100000 |
+----------+-------+
If you want to rollup (say Sum of amounts) for an employee from the beginning, you would join the fact and dimension using the ID key and group by emp_key.
So,
select emp_key, sum(amount) from employee dim, account fact where dim.ID = fact.ID group by emp_key.
If you want to find out the amount since he became a manager, you just have to do rollup on the ID field.
select dim.ID, sum(amount) from employee dim, account fact where dim.ID = fact.ID group by dim.ID.
or this way -
select fact.ID, sum(amount) from account fact group by fact.ID.

Ideal solution for the following case scenario in database

There are 50 exams to be written by around millions of students online, One person may or may not write more than one exam. A person can also write a single exam more than one time ( retries ) ..
So which of the below solution is better for this case, I am okay with a better solution than these two as well
Option 1. Store each exam in a single table :
Subject 1
+----------------+---------+
| student id | Marks |
+----------------+---------+
| 1 | 85 |
| 2 | 32 |
| 2 | 60 |
+----------------+---------+
Subject 2
+----------------+---------+
| student id | Marks |
+----------------+---------+
| 1 | 85 |
| 2 | 32 |
| 2 | 60 |
+----------------+---------+
Like above with each table will have the student id only if that particular person has taken that exam , and have multiple occurrences of the student id if he has taken it more than once.
Option 2 :
+----------------+---------+---------+
| student id | Subject | Marks |
+----------------+---------+---------+
| 1 | Subj1 | 85 |
| 2 | Subj1 | 32 |
| 2 | Subj1 | 60 |
| 1 | Subj2 | 80 |
| 3 | Subj2 | 90 |
+----------------+---------+---------+
with all the values in a single table.
Which is better in terms of performance and storage perspective.
My various que
I think the best here is following:
Table STUDENT with information about students
Table EXAM with information about exams
Table EXAM_TRY with reference to STUDENT and EXAM tables, and fields DATE_OF_EXAM and RESULT_OF_EXAM
2 indexes on foreign keys in table EXAM_TRY
Depending on situation - index on date field (for example, you would need it for planning work for examiners)

testing foreign keys with cucumber

I'm trying to set up the background for a cucumber Feature. Ideally I want to be able to do:
Given the following folders exist:
| id | parent_id | name |
| 1 | nil | folder1 |
| 2 | nil | folder2 |
| 3 | 2 | folder3 |
| 4 | 1 | folder4 |
| 5 | 1 | folder5 |
| 6 | 5 | folder6 |
However I can't do this as I can't set the ID of a particular model and so the first row may be created with an ID of 7 and therefore none of the other "child" rows can access it. Name is not unique so I can't do a find_by_name in the step definition. I've got a feeling it's gonna be some ugly nested array solution.
Any ideas how to achieve this?
I don't understand why you can't choose unique names for the purpose of your configuring the test?
The way I ended up doing it in my step definitions:
Given /^the following folders exist:$/ do |table|
table.hashes.each{|f|
folder = Folder.new(f)
folder.save
ActiveRecord::Base.connection.execute('UPDATE folders SET id = '+f['id'].to_s+' WHERE id = '+folder.id.to_s)
}
end

Resources