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
Related
I'm starting to code a linkedin-like website in Rails for my thesis that would enable members to post job offers (employers) and other members (employees) to respond to them.
I've been trying to wrap my head around the data model and associations - because I don't want to have two channels of authentication for emploYers and Yees - I want to keep email, nickname and password in one table (model) and use that for logging in, and then from there go to Employer and Employee specific data.
I was reading about STI but I figured that Yer has very different data than Yee and there would be a lot of nulls in the Users table, which isn't quite an optimal thing I suppose.
Then I've stumbled across polymorphic associations, but I don't really know how to set them up.
I was thinking of something like this:
User < ApplicationRecord
has_one :employer, polymorphic: true
has_one :employee, polymorphic: true
end
Employee < ApplicationRecord
end
Employer < ApplicationRecord
end
My head boils cause I don't know what's the best way to achieve this, any tips much appreciated,
cheers
You don't need to use the polymorphic association here.
User < ApplicationRecord
has_one :employer
has_one :employee
def employer?
employer.present?
end
def employee?
employee.present?
end
end
Not sure if this would be the best solution in your case though.
I'm relatively new to Rails, so apologies in advance if this is an obvious question. I could not find an answer through searching.
I'm building a basic application which acts as an educational course - users can view lessons, take in the content, and mark lessons as 'complete' accordingly.
I would like users to be able to see which lessons are complete, by marking them as such in the course overview.
I have a Users model and a Lessons model, and the lessons are identical per user at present. If lessons were unique per user, this would presumably be solvable with a boolean 'complete' column for each lesson. This is not the case in this application, however - some users will have completed a lesson; others will not have.
How would I best go about a solution to this? All suggestions and ideas welcome.
Use many-many association and establish relationship among the 3 models.
class Student < ActiveRecord::Base
has_many :progresses
has_many :subjects, through: :progresses
end
class Subject < ActiveRecord::Base
has_many :progresses
has_many :students, through: :progresses
end
class Progress < ActiveRecord::Base
belongs_to :student
belongs_to :subject
end
To be precise use this link to get good understanding on many-many association :)
Third model, say Progress or Completion or Grades or UserLessons... that will belong to user and lesson, and have a field called complete or score or grade....
It's a classic case of many-to-many relation.
User can read multiple lessons
Lessons could be read/complete by multiple users
So to do this is to create another model with column user_id lesson_id as a foreign key for user and lesson and create an additional column completed boolean
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 :)
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.