Ruby on Rails App.
I have two kinds of users,
Company
has_many :employees #sample
Employee
has_one :company
I want to create friendships between these two models, keeping track of the requester of the relationship (.requested_relationships) and the receiver (.pending-relationships) as well as the status (accepted, pending, etc). I can easily create the relationship with a third model and has_many :through ... BUT that wouldn't allow me to track who initiated the relationship and thus distinguish between pending and requested relationships. What is the correct way to model this?
I've been playing around with a polymorphic attribute on the relationship model, but without concrete success.
you can try to use amistad gem
very good for friendship relationship and has a very good documentation that can help you out alot. just check it out
Related
I am wondering how do I add additional properties to a many to many relationship.
I have two models that share a many to many relationship, Company and Profession.
Many professionals could belong to a Company
So my Company model looks like below
class Company < ActiveRecord::Base
has_and_belongs_to_many :professions
end
The same people in the same profession could belong to multiple companies as well
so
class Profession < ActiveRecord::Base
has_and_belongs_to_many :companies
end
Now I need to associate an hourly rate which could be different for each of the companies for the same profession. I am not very sure where to introduce the hourly rate property? Even if I were to add that to the joining table, how do I access that rate using active record?
This is a typical scenario where you select has_many through over habtm. As a rule, if you only need to associate two models, no other info needed to be stored in the association, use habtm. For most cases, you have to use has_many through. You case falls under this scenario.
You want to save the hourly rate in the table that associates a Profession and a Company. If you have existing data that you want to migrate, you may want to look at this post How to migrate has_and_belongs_to_many to has_many through?. If you can drop the joins table you use for the habtm association, just drop it and create a new table.
I'm rather confused by the construction and have tried several ways to get the following situation to work for my test. But I can't get it to work.
This is what I want:
When an activity is being made. Several clients can be assigned to that activity. Therefore creating access to #oneActivity.clients or #oneClient.activities.
Should I put up a references :client in my activity migration or the other way around? And which of the two should have to belongs_to in the model and which the has_many?
well if a client has many activities and an activity has many clients then i suggest you take a look at has_and_belongs_to_many relationship.in that case
in your Client model you would have
has_and_belongs_to_many :activities
and in your Activity Model you would have
has_and_belongs_to_many :clients
that way you can do the actions you described in your question
You can check out relationships from the rails guides here: http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association
I guess, from what you describe, that you need a many-to-many relationship. Clients can have many activities, and activities can, as you describe, be assigned to several clients.
Setting up such a relationship is described in the following question When should one use a "has_many :through" relation in Rails?
in your Client model you would have
has_and_belongs_to_many :activities
and in your Activity Model you would have
has_and_belongs_to_may :clients
In my application, I have a User model and Artist model. Both users and artists should be able to "follow" multiple artists. I'm not really sure how to describe this relationship, and I'm completely lost regarding the underlying migration.
I've tried having has_and_belongs_to_many :artists in both classes, and an additional has_and_belongs_to_many :users in Artist, but that seems really messy, and I don't know how to write the migration.
I've looked at the association docs for the migration; I'm not sure if it would be simpler to have a third-class and use a :through association, or if I would need one for user-to-artist and another for artist-to-artist, or if one additional class would be enough.
Help appreciated, thanks in advance!
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 trying to understand and implement Active Record Associations in Rails and am having some trouble understanding how to put together the specific relationships I need.
I have a Recipe model and an Ingredients model. Many Ingredients will belong to a single Recipe and therefore, a Recipe will have many Ingredients. I am having trouble grasping how this is handled through MySQL and how to implement these relationships in the models correctly. Here is the (relatively sparse) code I have, so far:
models/recipe.rb
class Recipe < ActiveRecord::Base
has_many :ingredients
end
models/ingredient.rb
class Ingredient < ActiveRecord::Base
has_and_belongs_to_many :recipes
end
However, I'm fairly certain the association line in ingredient.rb is incorrect.
How would I correctly implement these relationships?
Your Recipe model should have a has_and_belongs_to_many relationship with the ingredients instead of has_many. This allows a single recipe to have many ingredients (i.e. you can do #recipe.ingredients), while a single ingredient can be in many recipes (#ingredient.recipes).
It does seem kind of weird when first starting out, but once you've grokked how Rails relationships work, it becomes intuitive. You're on the right track.
A has_many relationship implements a one to many mapping while has_and_belongs_to_many implements a many to many mapping. So a has_many is paired with a belongs_to while has_and_belongs_to_many are paired together.
The relationship between Recipe and Ingredients will be a many to many one if you also want to find out relations like which recipes a particular ingredient is being used in.
To implement a has_and_belongs_to_many in a mysql db you will have to create a third join table that maps all the links between the two tables.
You can look at this stackoverflow question to get a better idea of the format of the join table.