Im creating my own blog managing app in rails (for experimental purposes).... What would the best way to get this done?
I have posts and categories.
I want to have a dropdown of categories for the user to select one when they create a new post.
Now, each user will have different privileges so not all categories should appear for all users....
Right now Im at the point where I can create posts and choose which category I want... I havent added any filter per user support....
please help me on where should I go now??
First you will need to implement authentication and authorization. There are many good Rails tutorials on these subjects so I won't go into more detail here.
At this point you will have models for User, Post, and Category. You need a list per-user of authorized categories. A naive model:
class User < ActiveRecord::Base
has_and_belongs_to_many :categories
end
But that is misleading, because a user isn't actually "in" any categories. You probably want something like a join model like so:
class UserCategoryAuthorization < ActiveRecord::Base
belongs_to :user
belongs_to :category
// More fields here; possibly:
// belongs_to :authorized_by, :class_name => 'User'
end
class User < ActiveRecord::Base
has_many :user_category_authorizations
has_many :authorized_categories,
:through => :user_category_authorizations,
:source => :category
end
To start with I would give Users a has_many categories relationship(Which you could turn into its own model object at some point if this idea gets more complicated..or now if it already makes sense) and then assuming you already have log in functionality you can ask the logged in user for its categories and populate the drop down appropriately.
If this is a security issue rather than just convenience then you will need to validate the chosen category is in the users categories when the form is submitted back to the server.
If you don't already have logins I believe there are several rails plug-ins that attempt to help you get this functionality quickly.
Related
For a recruitment application, I have a user model, which has many common_app.
Each user will actually only have 1 common_app, but in case in the future they have more, I'm setting it up as a has_many association.
In the common app, I'm thinking of having the following data ->
user_id:integer
current_city:string
grad_year:integer
read_type:string
listen_speak:string
time_in_china:integer
desired_industry:(not sure, will be a multi-select picklist)
location_pref:(not sure, will be a multi-select picklist)
What I'm confused with doing this though, is that a part of the functionality of the app is to filter users based on their answers.
With this type of association, will I be able to filter all users, based on their answers? i.e all users whos grad_year is 2005 ?
If yes, how would I write the command to do that?
class User < ActiveRecord::Base
has_many :apps
end
class App < ActiveRecord::Base
belongs_to :user
end
User.includes(:apps).where('apps.grad_year = 2005')
Please forgive me if this has been answered already but I've searched a lot and still require some clarification.
I'm building a Property Management tool using Rails 3.2. I already have a User model and authorisation/authentication code in place with full tests coverage.
I've just started drawing out the models/classes/tables and have gotten myself a bit confused.
Let's start with Users.
Modelling Users
I plan to have allow multiple companies to use this system. Each will have employees (users). These users will have different roles e.g. Manager, Agent, Accountant, Secretary etc. For the most part the data I plan to store for each of these users will be similar (or so I think at the moment) so I am leaning towards Single Table Inheritance and using the type to define the level of access each employee has.
Secondly, I plan to allow Landlord and Tenants to also log in to the system. Upon logging in they'll be able to view information about the property they are owning or renting - maybe keep their contact details up to date too.
I was thinking of using polymorphic associations to represent these users.
So the plan I have at the moment and would like some feedback on is to have
User < ActiveRecord::BASE
Employee < User (this will have a STI type column and allow for the different employee roles)
Landlord < User
Tenant < User
Is this the best way of approaching this problem or am I shooting myself in the foot?
I've had some people advise me I should have a 'roles' table and assign roles to the users - but I have a feeling this isn't the most elegant way to do this in Rails.
Properties
My next issue is with Properties. Right now I have a model for properties and when I add them they belong_to a User (i.e. they have a user_id foreign key). I then started thinking "what happens if the employee (user) that added the Property leaves the company or has their account deleted for some reason?"
So in this scenario is it best to forgo the User/Employee to Property association and just link the Property to the Company that the employee belongs to? This way I can all employee.company.properties to list out all the properties?
Landlord and Tenant associations
Let's presume we make Properties belong to a Company.
In terms of associations this is what I have in my head. Looking at it now I see that everything belongs to a company because one company using the system shouldn't be able to see the landlords/tenants/employees/properties of another company.
class Landlord < User
belongs_to :company
has_many :properties, :through => :ownerships
end
class Tenant < User
belongs_to :company
has_one :property, :through => tenancies #you can only live at one place at a time right?
end
class Property < ActiveRecord::Base
has_many :tenants, :through => :tenancies
has_many :landlords, :through => :ownerships
belongs_to :company
end
class Company < ActiveRecord::Base
has_many :properties
has_many :employees
has_many :landlords :through => :ownerships #not sure if through is required/works here
has_many :tenants :through => :tenancies #not sure if through is required/works here
end
class Employees < User
belongs_to :company
end
Properties
Also I'm guessing we'll have different types of Properties (Commercial/Residential) and of those there will be whole buildings, apartments within a building (single address) etc.
Like with users I'm planning on using Polymorphic Associations to define two subclasses CommercialProperty and ResidentialProperty and then use sti to define a type. If the type is "multi unit" then have a new model for units and an association whereby Property has_many Units and a Unit belongs_to a property.
Aim
I'm trying to make sure that my code follows best practice as much as possible so that when I come to add new features and expand I'm not left having to re-write large chunks of the app.
I would really appreciate your feedback.
Reference
Some of the posts I've read. Hopefully to help others trying to solve the same problem.
Designing a Rails application: single table inheritance?
Ruby on rails with different user types
Ruby On Rails User Model for multiple types
It's probably too late but you could also use has_and_belongs_to_many on User and Company and thus avoid STI altogether by using gems cancan and rolify.
It allows you to define finely grained access rights (abilities).
I know that it seems more elegant having different classes instead of roles, but it is not viable long-term strategy, it can become messy when logic becomes complex.
Other then that, the rest seems pretty solid, hope that helps :)
I'm about to add a +1/likes feature to a personal project I'm working on. I'd like to know what the database design for this might look. I don't want to have several like tables but one like table to deal with all likes through out the website. I'd like to be able to "plug in" different parts of the site as they're created or at will.
E.G.
Microposts - users should be able to "like" microposts
Photos - users should be able to "like" photos
Let's say I create a games section and want users to be able to like games I want to be able to use the existing likes table instead of creating a likes table for games.
At the back of my mind I'm thinking the acts_as_tree plugin will be quite useful but could keeping things in the one likes table get messy after a while?
How can I add the likes feature to my app in the most sensible scalable way?
Take a look at polymorphic associations.
The example in the link provided talks about comments, but I think you want the same thing, with likes.
class Like < ActiveRecord::Base
belongs_to :likable, polymorphic: true
end
class Micropost < ActiveRecord::Base
has_many :likes, as: :likable
end
class Post < ActiveRecord::Base
has_many :likes, as: :likable
end
Here, both your Post and Micropost classes can have likes. And you can add some to any classes you want.
You can even do Like.all and, for each like, get the related object (a Post or a Micropost) with like.likable.
you should have a "Like" model or something, with, at least, this associations:
belongs_to :user
belongs_to :liked_object, :polymorphic => true
now you can do
Like.create :liked_object => micropost, :user => current_user
and in a future
Like.create :liked_object => game, :user => current_user
search for polymorphic associations (you may need a liked_object_type and liked_object_id on your migrations)
I have a rails app that is tracking social data. The users are going to be able to create groups and add pages(ie. facebook fan pages) to their groups by the page's social id. Since users could potentially be adding the page as someone else, I have it set up so that there is only one page per social id. I also have a pivot table called Catgorizations that links of the pages to the groups and users.
My model relationships are set up as follows:
User
has_many :groups
Group
belongs_to :user
has_many :categorizations
has_many :pages, :through => :categorizations
Page
has_many :categorizations
has_many :groups, :through => :categorizations
Categorization
belongs_to :group
belongs_to :page
Now when I create a new page and it saves, it is creating a new categorization. The problem I'm running into is that in the Categorization I need to set the user_id manually. I've tried:
#page.categorizations.user_id
But I get an undefined method user_id error. I may be approaching this from the wrong direction but how would I go about setting the user_id of a categorization through the page object?
Also if it matters, I'm using devise to handle my user management stuff.
Any help would be greatly appreciated.
Thanks
What you're tring to do is to access something several levels deep in a 'chained' set of relationships.
In order to access an instance of the User model, given a page, you need to get its categorizations, pick one (how?), get its group, then see what the user_id is of that group.
Conceptually a page will actually have many users that might be in charge of categorizing it.
To get something to happen you could arbitrarily pick the first categorization and then do something with its user:
cat = #page.categorizations.first
user = cat.group.user
I don't know what you mean about 'setting' the user id for the categorization - it doesn't have a user, so I don't know what you'll then want to do with that information, sorry!
From your description and your model, only Pages have a User, not Categorization, which is why you get the error that it doesn't exist (it's not in your database).
Also, you are missing the opposite association on Page:
Page
belongs_to :user
This allows you to get back to User from Page:
#page.user.id
I am working on an application where a user has the ability to leave feedback on another user. Currently, I already have an implemented user model.
Is this considered a self referential association?
This seems a bit wierd to me (how to set it up through Active Record Associations).
How would I go about setting this association up, what type of association is it? Anything I need to watch out for while going about this (tips, suggestions, errors that may come up)?
According to Answers Below Would This be the Way?
(one to many between users and ratings and one to one between ratings and user)
class User < ActiveRecord::Base
has_many :ratings
end
class Ratings < ActiveRecord::Base
belongs_to :user
end
This may not be in Rails/Active Record terms but ORM generic.
I would probably implement this by having a Feedback or Review model and your User model would have one-to-many Feedback/Reviews and the Feedback/Review would have a one-to-one relationship with the User who created it.
EDIT: Response to question update...
You can have an User object on the Ratings model that is the author. My rails experience is minimal but I believe your Ratings model would look like this.
class Ratings < ActiveRecord::Base
belongs_to :user
has_one :user
end