Rails 3 multiple has_and_belongs_to_many relationships - ruby-on-rails-3

I'm creating a rails app that contains lists. Lists are made up of:
List_Source (e.g. National Geographic)
List_Elements (e.g. Tanzania)
List_Tags (e.g. Travel)
A list has a single source, multiple elements, and multiple tags. A source has multiple lists. A tag is used for multiple lists.
I have models/tables for lists, sources, list_elements, and tags. How should I organize the tables so that I can pull into one master table of lists the source, elements, and tags?

Railscast has the answer here. Basically you create a new model (e.g. Listable) and then use has_many_through for each of the other models to create this many to many relationship.
http://railscasts.com/episodes/47-two-many-to-many

Related

Association table between 2 association tables. Is this the correct approach?

There are 3 initial tables, Product, User, and Tag. (Not really a product table but it makes for a simpler example)
There's a many-to-many relationship between a User and Product, as well as between a User and Tag. So, I created 2 association tables for this relationship -> UserProduct and UserTag
Now, a user can create multiple tags, and add those tags to a product. From my knowledge, this can be achieved by creating another association table, UserProductTag, between the association tables UserProduct and UserTag.
I wasn't able to find many examples of this use case on the internet. That made me wonder, is this the correct approach? Or am I overdoing normalization?
This gets further complicated by the fact that I've to achieve this in SQLAlchemy as well and I've no idea (but I guess that's a different question).
I believe what you are looking for is what is often called "ternary relationship".
Please see these two SO questions and answers to give you implementation ideas:
How to three-way many-to-many relationship in flask-sqlalchemy
Inserting relationships into a table which connects 3 tables with many to many relationships with SQLALchemy - python

PostgreSQL full text search across multiple tables

I have a requirement that need to support autocomplete from UI and search across multiple tables for possible match. For example, I have 2 tables: Recipe and Ingredient. When a user types in 'Chicken', it should return all recipe that contains 'Chicken' and all ingredients that contain 'Chicken' (e.g. minced chicken, diced chicken, etc).
I think I need to use full text search but I am quite new to this. I can find many examples online that show how to do it for a single table, but not across multiple tables. Do I need to extract contents from multiple tables into a separate table just for search? Or does postgres support full text search across tables?

When do relationships in ERD diagrams get a separate table in a RDBMS

Title is the question. When should a relationship in an ER diagram be given its own table in a RDBMS? For instance, one mail courier(with attributes eid and surname) can deliver a number of packages but a package(attributes,pid, sent_By, going_to) can only have one mail courier. Would it make sense to make a table for the relationship called delivers(with an attribute of the time that the package was delivered)? or should the eid of the mail_courier and time_delivered from the deliver relationship be added to the package entity? Also, what would be an example when you would not want to add the attributes to the package entity?
I think what you are trying is to create a one-to-many relationship between two entities. And for that, there is no need to create a separate table; as you mentioned in your question, just add those two attributes to the package table.
Where you would need to create a separate table is when you want to achieve many-to-many relationship between two entities. For example, take twitter's followers. One user can have many followers and a follower can follow many users. You can't do that the relational way without creating a new table with just those two columns.

Hbase and 1- Many Relation

I have one question which can be best described by the following scenario.
Suppose I have three tables BaseCategory,Category and products. If i am thinking in terms of RDBMS then the relationship amoung these tables are
1- One BaseCategory has Many categories
2- One Category has Many Products.
Now i am thinking to convert it into HBase. can anybody help me how to map these relations into HBase?
You'd probably have each row represent a supercategory/category pair (encoded with a separator, e.g. MySuperCategory:MyCategory, and a column family named "products" with a column for each product in that category.
This would allow you to very quickly retrieve all of the items in a given supercategory/category pair, and with some de-duplication all of the items in a supercategory.

How to add extra field in the ManyToMany generated table with FluentNHibernate

I have 2 entities Users and Colors. One user may be associated with multiple colors, and one color may be associated with multiple users.
I use FluentNHibernate autopersistence model to generate the database. Now I have to add to the generated table UserToColor(UserId,ColorId) also an extra field 'CreatedOn', how can I accomplish and is this accomplishable with fluent-nhibernate?
NHibernate doesn't support this. The best workaround is to create an extra class as a link table, and have a many to one relationship to it from each of your existing classes.