I have a class User and I would like to implement the follow relation (as in twitter).
In my model a user can follow a set of other users, and also can be followed by a set of users.
In the database, there are two tables: User and Follow.
Follow has src_id and dst_id, both foreign keys to the User table. An entry in the follow table means that user with id=src_id is following user with id=dst_id.
I am having difficulty expressing this in the actual models...
thanks!
This should be what you're looking for: http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#cha-following_users
For a more expansive friendship model where users can request to be friends, show pending friends etc. you could try this gem https://github.com/raw1z/amistad and this is a really good tutorial on how to get it working: http://keighl.com/post/amistad-friendships-controller
Hope that helps!
Related
I am working on a web application written in ruby on rails
There is a model of courses that exists in the database.
A user has the ability to save courses that he/she plans to take.
I accomplish this by having a current_user.courses table. (courses_users)
However, if a user wishes to remove a saved course, and I issue a delete request with that id like so
current_user.courses.find_by(id: params[:id]).destroy
I end up deleting both the entry in the joined table and the entry in the courses table. I confirmed this by looking at the server logs and found
DELETE FROM `courses_users` WHERE `courses_users`.`course_id` = 219
SQL (0.4ms) DELETE FROM `courses` WHERE `courses`.`id` = 219
Doing the two actions described above when I only want the first.
Is there a way to just remove just the entry in the joined table? Or does that go against the nature of a joined table? If so, is there a more efficient way of achieving this functionality?
Any help would be appreciated and if you would have me post anything else please ask.
Thank you,
EDIT: The relationships between the two models:
a user has_and_belongs_to_many courses
and a course has_and_belongs_to_many users
Your code is saying to destroy the course, so that's what ActiveRecord is doing.
You want to delete the association, not destroy the user nor the course.
The solution is to use collection.delete(object, …)
This removes each object from the collection by deleting the association from the join table.
This does not destroy the objects.
Example code:
def delete_user_course_association(user_id, course_id)
user = User.find(user_id)
course = user.courses.find(course_id)
user.courses.delete(course) if course
end
See Rails API ActiveRecord Associations has_and_belongs_to_many
The line below deletes the courses record with id params[:id]
current_user.courses.find_by(id: params[:id]).destroy
I think you meant to do:
current_user.courses.delete( params[:id] )
My DB has following tables:
Resource: Some resources can be uploaded on site
Groups: Groups on site
Users: Users on site (not necessarily be part of any group but could be if they like)
Now, when some one uploads a resource then currently, ownership of that resource is given to it's uploader by default. So resource table has column OwnerID with foreign key association to User table.
But now, this has to be changed such that ownership of a resource could be given to either a user or entire group.
I'm trying to decide the migration scheme, to move this owner being user to an entity that could be either user or group. Intuition is that when someone uploads a material, he can choose its owner to be a user or entire group.
Currently my migration plans involves:
Add OwnerType (User, Group, Global) and UserOwner and GroupOwner within the Material table (probably worse normalized table).
OwnerType could be Global if owner is everyone --or-- Group if owner is group entity else user.
Then when I'm querying the Resource table, I can check the OwnerType to condionally select its Owner from either user table or group table.
I do not know if this is good way. I'm using entity framework, and things are already started to look ugly as User and Group hardly have any relationaship that would require me to make generalized entity.
Can some expert guide me on this? What is generally considered good migration plan in this case? Thanks for any help or suggestions.
I'm designing a web application for a school. So far, I'm stuck with the database which has these tables:
users
id
username
password
profile
user_id (FK)
name
last_name
sex
group_id (FK)
(other basic information)
... And other tables irrelevant now, like events, comitees, groups and so on.
So, the users table stores basic information about the login, and the profiles table stores all the personal data about the user.
Now, the *group_id* column in the profile table has a foreign key that references the ID column of the group in which the user is currently enrolled, in the groups table. A user can only be enrolled in one group at once, so there's no need for any additional tables.
The thing is that it doesn't make much sense to me declaring a relation like group HAS MANY profiles. Instead, the relation should be group HAS MANY users, but then, I would have to put a *group_id* column on the users table, which doesn't really fit in, since the users table only stores auth information.
On the other side, I would like to list all the users enrolled in a group using an ORM and getting the a users collection and not profiles. The way I see it, is that the users table is like the 'parent' and the profiles table extends the users table.
The same problem would occur when setting attendances for events. Should I reference the profile as a foreign key in the events_attendance table? Or should I reference the user ID?
Of course both solutions could be implemented and work, but which of them is the best choice?
I have dug a little and found that both solutions would comply with 3NF, so in theory, would be correct, but I'm having a hard time designing the right way my database.
This is a question of your own conventions. You need to decide what is the main entity, right after that you can easiy find a proper solution. Both ways are good, but if you think of User as of the main entity while Profile is a property then you should put GroupId into User, otherwise, if you mean User and Profile as a single entity, you can leave GroupId in Profile, and by this you're not saying group HAS MANY profiles but group HAS MANY users.
By setting a proper one-to-one relation (User-Profile) you can force your data integrity good enough.
I'm trying to figure out how I can use the Rails console to get the number of users that have two particular associations. Here's my conditions:
A User can have many DropboxUser accounts linked and many DriveUser accounts linked to their account via a foreign key. In my model declaration, both DropboxUser and DriveUser belongs_to :user. In conjunction with the previous statement, a User has_many :dropbox_users and has_many :drive_users. I know I have all of my models linked, so now I want to know how I can find out how many User accounts have both a DropboxUser account and a DriveUser account associated with them.
Here's some of the code I've tried (unsuccessfully):
User.count(:conditions => "dropbox_users.uid='user.id' AND drive_users.uid='user_id'")
Perhaps I was headed in the right direction with my last query, perhaps not. It told me no such column: dropbox_users.uid.
You have to join dropbox_users to be able to query on it.
User.joins(:dropbox_users).joins(:drive_users).count
Also, since joins uses an INNER JOIN, it will only return records that have both Dropbox and Drive accounts.
You can see my models here:
https://gist.github.com/768947
Just to explain what is happening, I have a few models. At the core of my app, there are: projects, stages, uploads, comments, users. Then there are roles & assignments to manage user authorization.
I am doing this with the plugin declarative_authorization & devise for login.
So the first question is, is it better to just add a column of 'Roles' to my user model/table and store the roles for each user there? If I have a user that has multiple roles, then I can just store all the roles as an array and cycle through them as needed.
Or is it better to do it like I have it setup now, where I use two separate tables and a bunch of joins to setup the assignments? I only have 4 roles: designer, client, admin, superuser.
Better in the sense that it is 'less expensive' from a computing resources standpoint to do each query with the column, than with the joins or is the difference not that significant?
I guess, the root of my question is...right now if I want to get a project assigned to the current_user I simply do current_user.projects.each do |project| and cycle through them that way. This is after I have done: #projects = current_user.projects in the projects controller. The same applies for all my other models - except Users & Roles.
However, if I wanted to find a user with role "client", it becomes convoluted very quickly. Or am I overcomplicating it?
Any help would be appreciated.
I think it's better to have user and role tables that are separate. It's a many-to-many relationship, because a user can have many roles and many users can have the same role. You'll need a JOIN table (e.g. user_role) to do that. I'd recommend three tables. Of course you'll have primary keys for both user and role; the user_role table will have two columns, one for each primary key, and foreign key relationships to their respective tables.
So now if you want all users with the role "client", it's an easy JOIN between user and user_role. If you want a particular user's roles, you'll need to JOIN the three tables.
I would not recommend an array of roles in user. It goes against first normal form.