Following functionality - database design problem - Rails 3 - ruby-on-rails-3

I need following functionality in my app (twitter like). One user can fallow other user.
I have model User, and I tried with self many-to-many relation, but I don't know how to implement this in my model.
Can you explain me some example how to do this?

Michael Hartl's tutorial has an entire section on follower relationships. I recommend reading it to get a better understanding of self many-to-many relations. It helped me a lot:
http://ruby.railstutorial.org/chapters/following-users
You could also use a gem like acts_as_follower, which abstracts much of the design details out for you:
https://github.com/tcocca/acts_as_follower

Related

Database MN relationship

I'm taking a class on database management systems (absolute beginner) and I'm working on a database for a very simple blog system.
I have a question regarding one M:N relationship between blog posts and categories where the posts belong (one blog posts can be in several categories.)
The part of the scheme looks like this:
Scheme
I know that somehow this scheme allows to add a blog post that doesn't belong to any category. However, I don't know why that is. Could someone please explain this to me?
Thanks.
It's probably a combination of two things. One would be a lack of referential integrity in your database design, ie you need foreign keys. The other would be that your front end application is allowing blogs without categories to be posted.
Because you can add a blog_posts record without having to add an associated post_cat record.

Core Data Relationships Binding in IB

I have a simple program that stores purchase information in a core data store. The model is similar to the following.
Entities:
Student(lastName,firstName) relationship to (BoughtPackage)
BoughtPackage relationship to (Payment) and (Package)
Payment(type,amount)
Package(name,price)
Structure looks like
I would like to first display the list of students in a table, then format the purchases made my that individual student in a details table.
So far, I have been able to create an NSArrayController to hold the data of the Student entity, but cannot figure out how to propagate the purchase detail table. How would I accomplish this using IB alone, or is it possible? If so, how would I do it programmatically?
Yes it is possible to do this mostly in IB without writing any code. But thats too broad a question for a SO answer
You really need to read
https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/cocoabindings/cocoabindings.html
and theres a tutorial in the docset titled
Introduction to Developing Cocoa Applications Using Bindings: A
Tutorial
which i can't find a web link for. Just search the title.
But sort out your model too. It looks like all your relationships are one-to-one
For example. I would expect that a Student may have many BoughtPackages hence a one-to-many relationship there and the same with BoughtPackage to Payment.
Good luck

The Rails way for databases: person may_have (has_one ?) account

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.

Retrieving data across 5 tables in has_many :through

Issue: Is there a better way to model the following or create a basic recommendation system than the database diagram below? If an extremely lengthy answer is necessary, you could instead just point me in the right direction and suggest things to research further.
I'm building a rudimentary event recommendation system by allowing users to answer questions and storing their user-answer relationship in the responses model. Each question's answer relates to a tag. Each event is also tagged. Thus, I should be able to provide users with recommended events via selecting matching tags and using this 5 table relationship. However, it seems like I would have to go through several has_many :through relationships in order to accomplish this, which I don't believe is preferred using Rails.
Would it be better to instead create a relationship from users to events via a background rake task or something, computing the relationship after questions are answered? Am I missing the concepts completely here and looking at this from the wrong angle? Eventually this system would be replaced with a more robust algorithm, perhaps using Mahout or something, but for now I'm just trying to get a simple proof of concept working.
Here's a link to the database diagram: Database Diagram

Nhibernate and History Tables

I am working on a ASP.NET MVC website using Nhibernate as my ORM. The project is similar to a wiki/blog engine and requires that as pages are edited they store a history of the edits in another table which can then be viewed and recovered. This is complicated somewhat in that each "page" can have collections associated with it that can also be edited/added/removed. I would also need to stored these changes.
I was wondering how this fits into an entity mapping scenario such as Nhibernate and how this might be implemented. If anyone knows of any articles on this, or has done this themselves then please let me know.
I was considering triggers but I would prefer not to mix data access technologies if possible. I also am using MySql so CDC would not be possible for me.
Thanks
Either implement an auditing interceptor or use the event system. The event system is newer, I haven't found any auditing examples yet...
Also see this related question:
Take a look at NHibernate.Envers https://bitbucket.org/RogerKratz/nhibernate.envers