In the models of my app I want to have the option that the owner of each record is a user or a company...
Normally I have a user_id field (user model) in each model for keeping the owner...
What are the implementation options for this?
How can I design this?
Should I add another field in the models? owner? how can I use the user_id (user_model) or the company_id (company model)?
The data in the app could be:
Personal data of the specific user
Company data, which are separated from Personal data. Ofcourse company data also created by a certain user which is a member (with certain role) of the company...
Any ideas?
Thank you
this can be done with polymorphic associations, you have a model that can belongs to more than 1 model with the same relationship, see this example
class Picture < ApplicationRecord
belongs_to :imageable, polymorphic: true
end
class Employee < ApplicationRecord
has_many :pictures, as: :imageable
end
class Product < ApplicationRecord
has_many :pictures, as: :imageable
end
for this to work you need to create an imageable_id and imageable_type on your picture table, fir this example.
you can check more information about this on rails documentation
Related
I have two models User and Project which are in has_and_belongs_to_many association
The user model has a column status
i can access the status of a user who is in project like
project.user.status
but a user can be in different projects i want his status to be on project level instead of on his id
If I understand your question correctly, the problem is that you need to associate the status of the user to one of potentially many projects that the user is associated with, but you are associating a single status to a single user instead of the project.
In that event, you need to abstract this association to an additional model, for instance "UserProjectStatus" which would be associated with both the User and the Project. You can do this using the has_many, through association. This would end up with something along the lines of:
class Project < ApplicationRecord
has_many :user_project_statuses
has_many :users, through :user_project_statuses
end
class UserProjectStatus < ApplicationRecord
belongs_to :user
belongs_to :project
end
class User < ApplicationRecord
has_many :user_project_statuses
has_many :projects, through :user_project_statuses
end
There is a good overview of this any many other Rails ActiveModel associations at https://guides.rubyonrails.org/association_basics.html#the-has-one-through-association.
Hope this helps!
I'm new to Rails and working with databases, and am wondering what the best way to set up the following Active Record Association would be.
I have the following tables:
user
product
request
I need each table to relate to the other tables in the following manner:
Every user should be able to have many products. Products will belong to users.
The products table needs to be associated with the user table in a way that no request is necessary to access product data through an association with user data
Every request needs to have two users, defined by foreign keys as requester and requestee
Every request also needs to have two products, defined as requester_product and requestee_product
I initially thought I could use a has_many :through association (seen here under 2.4: http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association), but this structure won't allow accessing product data through user data without the existence of a request.
class User < ActiveRecord::Base
has_many :requests
has_many :products
end
class Product < ActiveRecord::Base
has_many :requests
belongs_to :users
end
class Request < ActiveRecord::Base
belongs_to :requester, class_name: 'User'
belongs_to :requestee, class_name: 'User'
belongs_to :requester_product, class_name: 'Product'
belongs_to :request_product, class_name: 'Product'
end
I've assumed that multiple users can be associated with multiple products. Giving you:
user.products
product.user
request.requester # will return the relevant requesting user
request.requestee # will return the relevant requestee user
request.requester_product # will return the relevant requesting user's product
request.requestee_product # will return the relevant requestee user's product
product.requests # all requests associated to a product
user.requests # all requests associated to a user
I have a model named feedback with attributes, rating and review. A product may have a feedback, a appointment may have a feedback, a user may have a feedback.
I could add individual fields, like user_id, product_id, appointment_id in the feedbacks table, but that does not seem to be a very good scalable solution. The other option in my mind is to use a generalized, reference_id field, and a reference_model field which can be populated with the model name converted to string, and then I could manually interpret if the id entered in reference_id belongs to a user, product or an appointment.
What would be the best way of going about associating each feedback with any one of the 3 models, at a given time?
You should read about Polymorphic Associations.
class Picture < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
class Employee < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
class Product < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
There's even a nice RailsCast.
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 have following:
User model with columns:
id user_id password created_at updated_at
Store model with columns:
id store_id store_name create_ad updated_at
Basically a user can belong to multiple stores. So I would want to get a query like "get all the stores that the user belongs to"
Relationships I've made are:
class User < ActiveRecord::Base
belongs_to :store, :foreign_key => "store_id"
end
class Store < ActiveRecord::Base
has_many :user, :foreign_key => "store_id"
end
are these correct?
Ultimately I want to find out whether a userid, password and storeid should be able to login.
So how can I use the find_byXXX on this ? so If I get a row back with passed in userid, password and storeId...I would know whether user should be able to login?
I noticed that belongs_to and has_many questions have been asked before but I was not able to understand well from those questions. maybe answers specific to my problem will help...
You're looking for a has_and_belongs_to_many relationship. Your tables and model should be as follows:
User Table:
id password created_at updated_at
Store Table:
id store_name created_at updated_at
Join Table (called stores_users):
store_id user_id
In your models:
class User < ActiveRecord::Base
has_and_belongs_to_many :stores
end
class Store < ActiveRecord::Base
has_and_belongs_to_many :users
end
To get a user's stores:
User.stores
See the rails API for more information.
It seems like you're making a lot of false assumptions about how ActiveRecords works on a basic level, so I would recommend reading the official and very straightforward ActiveRecord Associations guide.
So you've said that a user belongs to many stores. How many users belong to a single store?
If the answer is more than 1, then what you need is has_and_belongs_to_many and a third database table. The table will essentially contain pairs of (store_id, user_id).
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
Although it is not required by Rails, it is recommended that you create a model for this relation and make the relation bidirectional on that model. You will thank yourself later.
class User < ActiveRecord::Base
has_many :userstores
has_many :stores, :through => :userstores
end
class Store < ActiveRecord::Base
has_many :userstores
has_many :users, :through => :userstores
end
class UserStore < ActiveRecord::Base
belongs_to :user
belongs_to :store
end