I am a rails newbie struggling to understand the advantage of using a polymorphic association over multiple belongs_to declarations with associated foreign keys. In Ryan Bates' railscast (http://railscasts.com/episodes/154-polymorphic-association-revised), for example, articles, events and photos can each have many comments, so he sets up a polymorphic association using commentable.
Why not just have comments belong to each of the other three assets and include article_id, event_id and photo_id foreign keys in its table where only one will be non-null?
You could do that, but there will be disadvantages. Some that I can think of:
Sparse table with many nulls
No separation of concerns. Comments has to change whenever you add a commentable model
Rails already supports polymorphic associations and makes it easy to use them. So why not?
Related
I feel like I have searched through the internet to find an answer to this question for quite some time now, but without success. Does anyone feel comfortable explaining why many-to-many relationships should be replaced with a bridge table?
Probably most (all??) RDMS implement a M:N relationship by creating a table containing two columns with the FKs.
So there is no advantage to explicitely model the bridge table.
But in most realistic cases you want to store additional information (besides the fact of its existence) about the relationship instance, e.g. timestamp and user from the creation. That means that you need to model the bridge table anyway.
I have a Rails Application and I have an SQL Database. I am using Oracle SQL Developer to manage it. My question is how do integrate these two together so that I can display the database data?
This is obviously a big subject, but the major steps I can think of are:
For each table in the database that you want to include in the Rail App, you define a model class that inherits from ActiveRecord::Base
If the database does not respect Rails conventions with regard to naming you will have to provide class methods in each model to define the primary key column and the table name.
You create associations in the models to describe the joins between the tables.
I'm trying to figure out most of the database design and normalization before I do much for my current project. Unfortunately I don't have much experience with database design, so it's a fairly slow process. One of the issues I'm trying to figure out is what's the best way to deal with a situation where one table may, or may not, be associated with another table.
A little background will help clear the question: I'm building a web application, using Rails 3.2, that helps manage races. People will be able to create accounts (/user accounts), host races, and manage the various aspects.
One thing is that the participants in a given race may or may not be users. In fact, we can assume that most of them will not be users. But for those who are, it would be nice to be able to link to their profiles (and, going the other way, link from their profiles to the races they've participated in).
It's sort of like blog posts where people can post anonymous comments, but if they do decide to log in and use their account then it's linked with the post in various ways.
I've searched for a while, but haven't really found solutions. I figure the way to do it is to have the Participants model note "has_one UserParticipation", which would usually be nil.
Is that a valid solution?
Is there a better way to go about this?
Here's a small diagram I threw together in Paint to concisely show the issue:
Question 2:
This is a little less important, but I figured I'd ask it in the same question because I've already posted the relevant question: several things will reference participants, is there any reason to set up a composite {Race_ID, Participant_Number} super key rather than always reference it using "race.participants"? (As far as I can tell, these would work very similarly.)
You may be over thinking it a bit. If I am following you correctly, this is a simple entity relationship diagram I whipped up in Dia:
Some explanation on the assoications of a User to Participants:
A Participant will have the belongs_to :user association, which is nil if there is no associated User.
A User will have the has_many :participants association, allowing none to many Participants relations. If there are none, a user instance will have user.participants equal an empty array.
As to the second question, you would only need to use both keys if you are querying for a specific participant for specific race, e.g. where participant_id = 7 and race_id = 4.
So a race has many participants (some of whom are users), and a participant has many races (hopefully :-).
Taking the user part of things out of the picture for a moment, this is a simple many-to-many relationship which Rails handles beautifully with has_and_belongs_to_many on both Race and Participant models, described here http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association. Another alternative, not necessary in your case is has_many :through which creates a first-class model backed by the join table. But what you have described makes this unnecessary.
The relationship between User and participant is one-to-one, and conditional. It's not clear to me if you can be a user without being a participant but if you have a User who is a Participant, you want them related. This is a :has_one relation.
The cool part of Rails that I'll bet you're looking for is that relationships can be conditional, so in this case a Participant has_one User conditionally. The linked Rails Guide document describes how to define all of this.
I'm working on a contact database in rails 3..
One thing thats really frustrating is how ugly the family relationship code is..
Is there a clean way of doing this in rails?
Basically all contacts are of the contact class (go figure!)
And contacts have many family_relationships (another model)
and many relatives through family_relationships.. The family relationship model also has one family relationship type (another model)
So far i've implemented this using the methods here http://railscasts.com/episodes/163-self-referential-association (using inverse relationships etc..)
But this just doesnt feel very clean.. and if i want to get all the contacts relatives, relationships etc.. i have to drop to raw SQL or join the arrays..
Is there a better (or definitive) way that this kinda thing is done in rails?
The Ancestry gem seems like it solves exactly this kind of problem:
Ancestry is a gem/plugin that allows the records of a Ruby on Rails ActiveRecord model to be organised as a tree structure (or hierarchy). It uses a single, intuitively formatted database column, using a variation on the materialised path pattern. It exposes all the standard tree structure relations (ancestors, parent, root, children, siblings, descendants) and all of them can be fetched in a single sql query. Additional features are STI support, scopes, depth caching, depth constraints, easy migration from older plugins/gems, integrity checking, integrity restoration, arrangement of (sub)tree into hashes and different strategies for dealing with orphaned records.
I've got the following business requirements for a scenario:
http://ompldr.org/vN2ZvdQ
and I came up with the following UML diagrams to model the relationship:
http://ompldr.org/vN2Y5Yg
I was wondering whether I have successfully managed to implement all the relations in the E-R model, especially the 1..* relationship. Furthermore, it is my understanding that mandatory relationships cannot be implemented just by using FK/PK so I have to add additional constraints, is that so?
Its seems to be ok. You can use ERWin or DBDesigner to doing your work in a easier way.
Consider ON DELETE CASCADE, ON UPDATE CASCADE, etc in your constraints.