ruby on rails database entry matching, storage
This is kind of a strange question, but I will try to explain it to the best of my ability.
Let's say I have the following two tables:
table: cars
+---------------+------------+----------------+--------------+
| color | size | capacity | origin |
+---------------+------------+----------------+--------------+
table: users
+----------------+---------------+------------开发者_运维技巧--------+-----------------+
| desired_colors | desired_sizes | desired_capacities | desired_origins |
+----------------+---------------+--------------------+-----------------+
Obviously the cars table will consist of unique cars with color, size, capacity and origin.
In the users table, there will be user selections of multiple colors they want, multiple sizes they want, multiple capacities they want and multiple origins they want.
Now, I'm wondering the best way to achieve a 'match score' between a users desired traits and each vehicle. This way, users can see the top matches of cars that they want.
I have a few ideas on how I would do this, but I was wondering what everyone here thought was the best way to go about something like this. I'm thinking the best way is to have a 'matches' table showing a match between a user_id
, car_id
and a match_score
-- but I'm not sure how and when I would run these calculations and save this data. Another thing to think about is if I decide to change the criteria for scores.
Anyways, I know this is a weird question and I might not get many answers, but just hoping to stir the pot and see if anyone has any good ideas.
Thanks all :)
I would just determine my matches in a query, rather than store it in a table. If your dataset is expected to be huge, it may be more efficient to pre-calculate matches, but in all likelihood, it would be premature optimization. So, what I would do is load the desired features when the user logs in, then run a query such as this:
select *,
if(color in ('green', 'red', blue', ...), 10, 0)
+ if(size in (...), 10, 0)
+ if(capacity in (...), 10, 0)
+ if(origin in (...), 10, 0) as score
from cars
where color in ('green', 'red', blue', ...)
or size in (...)
or capacity in (...)
or origin in (...)
精彩评论