I'm building a simple rails app that allows users to login and create private lists for themselves, so far so good. I would like the user to then have the option to invite people to a certain list where these new users would have edit access to the tasks. This would be in the same vein as basecamp and trello in regards to adding users.
Where would I begin to start with this I have been wracking my brains and searching google and can't find any relevant tutorials of guidance.
Ok, so what about having
1) A users Table (model)
2) A lists table (model)
Model Associations
A user has many lists
A list belongs to a user
User table will have Foreign Key list_id (you will need to add this via a migration)
Thats just a start, I am assuming you know about resources ( the big 7 ) in rails? As you will be using this extensively
You can create a Membership table which will act as a join table between a User and List. You can then add an :admin attribute to the Membership model with a boolean type.
class List < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :memberships
end
class User < ActiveRecord::Base
has_many :memberships
has_many :lists, :through => :memberships
end
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :list
end
Related
What I'm looking for is an appropriate way to set up a system where users can create projects and therefor become the admin of that project. The user can then add other admins to the project. Finally, other non-admin users can join the project.
I want to be able to verify whether a user is an admin of a project to check whether he has edit/update privileges. Any thoughts?
I figure I'll probably have a users_projects table and a projects_admins table, but I can't figure out how that translates to Rails relationships....
Thanks!
Ok, I will give it a try, but without too much code here in.
I see here 3 models:
User
Project
ProjectAdmin
The first 2 are simple models, with some attributes. The third one is the relation between the two and will be a n:m relation. So it is best to use the has-many-through relation here.
class ProjectAdmin < ActiveRecord::Base
belongs_to :project
belongs_to :user
end
class User < ActiveRecord::Base
has_many :project_admins
has_many :projects, :through => :project_admins
end
class Project < ActiveRecord::Base
has_many :project_admins
has_many :admins, :through => :project_admins
end
Of course you have to create additionally the 3 tables by migrations, and add later a similar relation for project users, named then ProjectUser as model. Have at least a look at the rails guide about relations, section "has-many :through".
To add the creator to a project, this should be a one-one relation between the two, so it should be sufficient to have:
class Project
has_one :creator, :class_name => "User"
end
(and of course the creator_id in the migration)
I have been doing some searching around Google and this site for some ways to go about constructing some models for an application I am working on.
I plan to have Users and Teams. A user can join a team and one team only. A team can obviously have many users. Also, I want to add a 'leader' role inside the team that is assigned to the User who creates a Team but can later be passed on to another User who is already a part of that team.
I have seen some examples of models for something like this and saw a few like this:
class User < ActiveRecord::Base
has_one :team
has_many :memberships
end
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :team
end
class Team < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :memberships
end
Why is the Membership model necessary? I have seen it in a few examples and I am not sure I quite follow the logic of why it is in there and what it is doing.
Also, if I did something like has_one :leader in the Team model and had the Leader model, would that be considered the best practice for determining a leader in a Team? It seems like a bit much to have an entire model/table for Leader.
The Memberships model is allowing for a many-to-many relationship there. It's acting as the join table. It would allow Users to belong to many Teams and Teams to have many Users.
Sounds like you just want a one-to-many though?
class User < ActiveRecord::Base
belongs_to :team
end
class Team < ActiveRecord::Base
has_many :users
end
I'll have to double check this part but you can use additional parameters to specify the models in your assocation if they don't match up with the name you need. So Team could have a leader that's just a User model.
class Team < ActiveRecord::Base
has_many :users
has_one :leader, :class_name => "User", :foreign_key => "user_id"
end
the rails guides page on associations has a good summary including the part about the :class_name and other options
http://guides.rubyonrails.org/association_basics.html#the-has_many-association
I've got a rails application built for schools. The users in the system are parents who track their childrens’ activities (aka family members or just members). Teachers are also users and can log in to participate in activities. The Member model is the place that the application uses to find participants. So that teachers can be found they are added to the Member model and for the same reason, parents are added as well.
A teacher added to this application would have one record in each table user, user_member, and member. A record in user to allow him to login, 1 record in member so he can be searchable, and 1 in user_member to make the association. In this case finding a user’s own member record is trivial. But if I were a parent with a login and 2 children in the system, James and Brian, one would find 3 records in the Members table, one for James, one for Brian and one for myself. I need to know which member record is mine, not James or Brian.
What’s the best way to model this? I’ve considered two options 1) there’s a foreign key in the user table that points to a user’s own member_id OR 2) the user_members table has a boolean called ‘own_member’ which indicates this user_id’s member_id is the member record for the user. Is there one that would be preferable? One more rails like? And how would I make the call build/create the associations?
The current relationships are modeled like this:
class User < ActiveRecord::Base
has_many :user_members
has_many :members, :through => :user_members
end
class UserMember < ActiveRecord::Base
belongs_to :user
belongs_to :member
end
class Member < ActiveRecord::Base
has_many :user_members
has_many :user, :through => :user_members
end
It sounds like the UserMember join model describes which members a user has privileges to access. This is a different concept than tracking which Member record is associated to a particular user. It sounds like a user would have a member record, and a member would belong to a user implying the following database structure:
class User < ActiveRecord::Base
has_many :user_members
has_many :members, :through => :user_members
has_one :member
end
class UserMember < ActiveRecord::Base
belongs_to :user
belongs_to :member
end
class Member < ActiveRecord::Base
has_many :user_members
has_many :user, :through => :user_members
belongs_to :user
end
This means adding another database column to the members table for "user_id". Creating and building the member record from a User instance could be done as follows:
user = User.new
user.build_member
user.create_member
More info on building through associations here: http://ar.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
You might consider creating callbacks to automatically create a member record on creation of a user record so you can skip manually building the associated record. An example would be:
# app/models/user.rb
class User < ActiveRecord::Base
before_create :build_member
def build_member
self.build_member
end
end
More information on callbacks here: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
I want to have a text field on a photo show view with autocomplete suggesting users. When someone selects a user from the suggestion list, the photo is posted to the selected user's profile.
I understand how to set up autocomplete, but how should I structure the display of the images onto the selected user's profile? Would this require a new model? Perhaps polymorphic if I want it to let users do this with multiple resources?
Can a photo be posted to multiple users' profiles? If so, then I would use the "has_many through" functionality that Rails offers for many-to-many relationships.
class User < ActiveRecord::Base
has_many :posted_photos
has_many :photos, :through => :posted_photos
end
class Photo < ActiveRecord::Base
has_many :posted_photos
has_many :users, :through => :posted_photos
end
class PostedPhoto < ActiveRecord::Base
has_many :users
has_many :photos
end
So you end up with a table of users, a table of photos and a table which joins the two.
See this blog post for a good explanation of Rails associations.
I have two models, Users and Groups. Each group can have many users and each user can be in many groups.
I currently have something simple like:
User:
has_many :groups
Group:
has_many :users
So I have a groups_users table which is just creating rows with group_id and user_id.
I want to add another column to this, (which I have), the question is how do I access it from a model without using a custom SQL call? In the group model I can go self.users and in user I can go self.groups
Is there a way to change the third column in this table from a user model?
Sorry if this is confusing, please advise on this
Here are a couple of tutorials that should help. Basically there two approaches to make many-to-many work, either has_and_belongs_to_many or has_many :through (recommended).
links:
http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off
http://railscasts.com/episodes/47-two-many-to-many
http://railscasts.com/episodes/154-polymorphic-association
In Rails 3 you want to make a join table for many to many relationships, using the plural names of the tables you want to join in alphabetical order. So in this case it would be groups_users.
models
class GroupsUser < ActiveRecord::Base
belongs_to :user
belongs_to :group
end
class User < ActiveRecord::Base
has_many :groups_users
has_many :groups, :through => :groups_users
end
class Group < ActiveRecord::Base
has_many :groups_users
has_many :users, :through => :groups_users
end
I [added] another column to [users_groups]...The question is how do
I access it from a model without using
a custom SQL call?
It sounds like you want to access a column of your user_groups table by calling a method on your User model or your Group model.
Some suggestions:
I'd name the table "user_groups" to work with ActiveRecord's pluralization expectations, but I'm not sure if that's essential.
Following Dave's advice, you'd want to set things up using the "has_many :through" technique...
# Declare a Model based on the many-to-many linking table.
class UserGroup < ActiveRecord::Base
belongs_to :user
belongs_to :group
end
class User < ActiveRecord::Base
has_many :user_groups
has_many :groups, :through => :user_groups
end
class Group < ActiveRecord::Base
has_many :user_groups
has_many :users, :through => :user_groups
end
Is there a way to change the third column in this table from a user model?
This is a little unclear, but keep in mind that each User can have a lot of UserGroups. So if you wanted to change that third column you'd have to find the particular one you're looking for.