Can friendly_id do his job when I have two related models? - ruby-on-rails

Here's the problem: I'm using friendly_id gem to create pretty urls. No problem on a single model but I have some related model eg Category and CategoryTranslation
Category
id parent_id
1 null
2 1
CategoryTranslation
id locale_id category_id description
1 1 1 Computer Science
2 2 1 Informatica
3 1 2 Algorithms
4 2 2 Algoritmi
So, in this case I should use field description of CategoryTranslation for my urls but this is needed in finders of Category controller. Is it clear? Someone can help me? Thanks in advance.

Related

Two references of the same model in another Rails: 4

I ask if it is good to have two references of one model in another and if it can do it how do I do it?
Problem: I have a record that will be controlled by two users (both always different users) and I need to have them see the records they have.
picture example:
example
The idea is that both can have access to the same registry and no one else can
you are trying to achieve many to many association, I would suggest you to use has_many_through association. you can read about it over here.
your tables will be like:
User:
id, email, name and other fields related to user
Box:
id, name and Fields related to boxes
User_Boxes: (Join table for mapping users with boxes, to do what you're trying to achieve)
id, user_id, box_id
so,
according to your diagram:
let the id's of boxes in box table be:
box home
box 2
box closet
box keys
Your mapping table would look like:
id box_id user_id
1 1 1
2 1 3
3 2 5
4 2 9
5 3 1
6 3 9
7 4 7
8 4 2
I hope this will solve your problem.

Order query by number of times one model has another

I have the following structure:
User model: id, ..
Event model: id, ..
UserMapEvent model: user_id, event_id
For example the UserMapEvent table might have the following records:
User_id | Event_id
1 | 1
1 | 2
3 | 1
4 | 1
I'm using this structure to save when a user likes an event.
Now, if I'm in the shows controller and I know the event's id, I can get the number of times the user liked the event like:
`likes = UsersMapEvent.where("event_id = ?", event.id).count`
How do I get, for example, the top 3 liked events? Do I have to join the 2 tables?
The only solution that I could think of is to use Event.all and for each to do the exact same thing, but it sounds stupid and I assume this is not one of those cases 'if it's stupid and it works is fine'.
What type of database operation do I have to use to get my query?
You may select top 3 liked events with next code:
Event.left_joins(:users_map_events).group(:id).order('COUNT(users_map_events.id) DESC').limit(3)

Rails: many to many relationship join table design

Model Material & Manager is many to many relationship,
this means Material needs to be checked by many managers,managers check many materials.
ManageMaterial is join table with attributes
material_id
manager_id
I load some data into join table like this
id manager_id material_id
---------- ---------- -----------
1 6 52
2 8 52
3 2 12
4 5 12
Now I want to show what materials are being checked, in this example I just want one item with material_id = 52, but if I do #items = ManageMaterial.all , it will return two items.
How to solve this problem?
If you want to get ManageMaterials without duplication:
#items = ManageMaterial.select("DISTINCT(material_id)")
If you want a single ManageMaterial with material_id:
#items = ManageMaterial.find_by_material_id(52)

Remove Many-to-many relations records in database with entity framework

If I have many to many relation ship in database witch is looking like that:
House-HouseType-Type
In data model it is looking like that
House-Type
I cant modify table HouseType directly because there is no entity for that.
For example if I have insert some entities in database after records in table HouseType looking like that:
HouseId TypeId
1 2
1 3
1 4
than I want to remove some relations records.
e.g Records in table after remove would be:
HouseId TypeId
1 2
How can I do that in Entity Framework 4 ?
house.Types.Remove(type)
or
type.Houses.Remove(house)

How to store word compositions in a relational database in a normalized way?

I'm trying to find a nice way to store word compositions of the following form:
exhaustcleaningsystem
exhaust cleaning system
exhaustcleaning system
exhaust cleaningsystem
The combinations are given by a default per case. Every word in a composition is stored as a unique row in table 'labels'.
labels
id value
--------------------------
1 exhaustcleaningsystem
2 exhaust
3 cleaning
4 system
5 exhaustcleaning
6 cleaningsystem
I thought about a new table called 'compositions':
compositions
id domain_id range
----------------------
1 1 2,3,4
2 1 5,4
etc...
But storing multiple separated values in a column isn't normalized design. Any ideas for that?
BTW: I'm using MySQL und ActiveRecord/Rails.
The design you propose is not even in first normal form, since range is not atomic
The schema I'd use here would be
compositions
id domain_id
-------------
1 1
2 1
compositions-content
composition_id rank label_id
------------------------------------------
1 1 2
1 2 3
1 3 4
2 1 5
2 2 4
with composition_id referencing an composition.id and label_id referencing label.id
The rank column is optional and should be here if and only if the range you define here is order-sensitive.
With this design, you have some referential integrity at DB level.
Well, this is as far as I can think of in terms of normalisation:
sets
id domain_id
--------------
1 1
2 1
etc...
compositions
id set_id label_id order
---------------------------
1 1 2 1
2 1 3 2
3 1 4 3
4 2 5 1
5 2 4 2
etc...

Resources