I am making a job application where the user registers and has a profile, can create companies and withing the companies jobs. To explain a bit easier, a simple LinkedIn.
User has_one Profile (thinking of using the gem Virtus or Cocoon so user can access attribute from Profile and not using accepts_nested_attributes_for)
User has_many Companies
User has_many Applications to many jobs
I somewhat know how the application should be modeled but not completely sure and would appreciate some feedback and help. I will post what I think it should be and hope to get some suggestions. Thanks in advance!
class User < ActiveRecord::Base
has_one :profile, :dependent => :destroy
has_many :applications
has_many :jobs, through: :applications
has_many :companies
end
class Profile < ActiveRecord::Base
belongs_to :user
end
class Company < ActiveRecord::Base
has_many :jobs
end
class Job < ActiveRecord::Base
belongs_to :company
end
class Application < ActiveRecord::Base
belongs_to :user
belongs_to :job
end
Related
I have a memberships resource and it belongs to user and club. I want to access the parent attributes i.e for club and user and I read that accepts_nested_attributes_for is used for parent side of a relationship. What should I write in my membership model?
I have searched about it both in stackoverflow and activeadmin docs but I did not get a thorough explanation about solving my problem...
My membership model is:
membership.rb
class Membership < ApplicationRecord
require 'csv'
belongs_to :club
belongs_to :user
end
Also what should i write in my membership resource which I have already registered with AA...
You can mention the following :-
1) has_many: memberships #in user model
2) has_many: memberships #in club model
This will help you access parent attributes from child model :-
membership.user, membership.club
Also, you can mention accepts_nested_attributes_for: memberships in user model.
When you write this, you can then build a common form for user and membership and modify both of them simultaneously. To achieve this, you will have to allow membership attributes in users_controller.rb.
The following should work(Similar question):
class Club < ApplicationRecord
has_many :memberships, :dependent => :destroy
has_many :users, :through => :memberships
accepts_nested_attributes_for :membership
end
class User < ApplicationRecord
has_many :memberships, :dependent => :destroy
has_many :clubs, :through => :memberships
accepts_nested_attributes_for :membership
end
class Membership < ApplicationRecord
require 'csv'
belongs_to :club
belongs_to :user
accepts_nested_attributes_for :club
end
I currently have three models: two different users models which support the Devise auth gem : community organizer and regular user, and one community events model.
I'm trying to allow the community organizer to create events :
To which users can donate funds to ;
Which users can attend .
I use stripe as the payment gateway. I've setup a controller/model scaffolding for the Event model so far :
class Event < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
belongs_to :org_user #organization_user
has_one :category
end
Here is my organization user model :
class OrgUser < ActiveRecord::Base
extend FriendlyId
friendly_id :username, use: :slugged
has_many :posts
has_many :comments
has_many :events
end
I was told that I may need these particular patterns inside of my events and org_user models
class Event < ActiveRecord::Base
event has_many :attendances
event has_many :users, through: :attendances
end
class OrgUser < ActiveRecord::Base
org_user has_many :attendances
org_user has_many :events, through: :attendances
end
class User < ActiveRecord::Base
end
What is your advice on creating the relationship. I also don't want to forget the User.rb which will sign the form to be a part of the event posted by the :org_user. Only the org_user will have the ability to create/post events. Normal Users will have access to the signup form to attend those events. Please, help me clear this hurdle.
Several things :
The way you right has many through relationship is wrong (see the doc )
You don't model your donation relationship from users to events
You could model things like this :
class Event < ActiveRecord::Base
has_many :attendances
has_many :users, through: :attendances
end
class OrgUser < ActiveRecord::Base
has_many :events # as coordinations
end
class User < ActiveRecord::Base
has_many :donations
has_many :attendances
end
class Attendance < ActiveRecord::Base
belongs_to :user
has_one :event
end
class Donation < ActiveRecord::Base
belongs_to :user
has_one :event
end
I'm a beginner in the wonderful Rails world, but I find some difficulties.
Here's my situation :
I have some Users, Users using Webapps, and appears a join table, "Quota", to track activities of each user on each webapp.
So, in the database table of Quota we find : user_id, webapp_id, quantity : int.
My models :
class Webapp < ActiveRecord::Base
#has_many :webapps_quota, :through => :quota, source: :user
has_and_belongs_to_many :quota
accepts_nested_attributes_for :quota
end
class Webapp < ActiveRecord::Base
#has_many :webapps_quota, :through => :quota, source: :webapp
has_and_belongs_to_many :quota
accepts_nested_attributes_for :quota
end
class Quota < ActiveRecord::Base
belongs_to :user
belongs_to :webapp
accepts_nested_attributes_for :user
accepts_nested_attributes_for :webapp
end
I'm seriously desperate about how I deal with this. How do I show his quota to an user for example?
Thanks for your help.
In order to add attributes in the association table you should use has_many through association:
class User < ActiveRecord::Base
has_many :quotas
has_many :webapps, through: :quotas
end
class Quota < ActiveRecord::Base
belongs_to :user
belongs_to :webapp
end
class Webapp < ActiveRecord::Base
has_many :quotas
has_many :users, through: :quotas
end
Your question: How do I show his quota to an user for example?
user = User.find(1)
user.quotas #display the quotas for that user
user.webapps #display the webapps for that user
In my rails application Company acts as the user model. A company can have many customers and many employees (carseller).
There is a many to many relation between a carseller and a customer.
There is the following problem: A lot of times i'd have to retrieve all the appointments made with the whole company. Since there is no company_id saved in the appointment model this can be quite painful.
Should i just include a foreign key to company in the appointments and have some form of redundancy or is there another easy and efficient way?
class Company < ActiveRecord::Base
has_many :customers
has_many :carseller
end
class Customer < ActiveRecord::Base
belongs_to :company
has_many :appointments
has_many :carsellers, :through => :appointments
end
class Carseller < ActiveRecord::Base
belongs_to :company
has_many :appointments
has_many :customers, :through => :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :customer
belongs_to :carseller
end
I think you can use :
class Appointment < ActiveRecord::Base
belongs_to :customer
belongs_to :carseller
has_one :company, :through => :carseller
end
Then you just have to do appointment.company to get it.
Can someone help me to correct my associations ?
I have the following models:
User, Developer, Application, Comments, Rating, Permission
Requirements:
A user can be a Developer or not.
A user can have Default Permissions and Permissions for each application
A user can install multiple Applications
A user can comment and rate multiple Applications
A developer can develop multiple applications
An application can request a list of permissions.
I already created some associations but I believe its not 100% correct or an easier way to do it exist.
Can someone suggest me a correct way to do it?
You are confusing models with authorization.
You should check out CanCan for role based authorization. For example you don't need your developer model since its just a user with a different role/permissions.
Edit: Changed 'role based authentication' to 'role based authorization'. As the comment below points out the difference between authentication and authorization.
I think this is what you want as far as your model work. You can use a join model to manage your application permissions, and use Rails STI to manage what each type of user can do, whether it's developing or not.
user.rb
class User < ActiveRecord::Base
has_many :apps
has_many :comments, :through => :user_comments
has_many :ratings, :through => :user_ratings
end
comment.rb
class Comment < ActiveRecord::Base
belongs_to :user
end
rating.rb
class Rating < ActiveRecord::Base
belongs_to :user
end
user_comment.rb
class UserComment < ActiveRecord::Base
belongs_to :app
end
user_rating.rb
class UserRating < ActiveRecord::Base
belongs_to :app
end
normal_user.rb (STI)
class NormalUser < User
end
developer.rb (STI)
class Developer < User
end
app.rb
class App < ActiveRecord::Base
has_many :permissions, :through => :app_permissions
has_many :user_comments
has_many :user_ratings
end
permission.rb
class Permission < ActiveRecord::Base
belongs_to :app
end
app_permission.rb
class AppPermission < ActiveRecord::Base
end
I agree with #Mark, don't use STI. The better way will be implement as suggested by #Chris Barretto except for STI and use CanCan for role based authentication. The change for User model will be:
class User < ActiveRecord::Base
has_many :apps
has_many :comments, :through => :user_comments
has_many :ratings, :through => :user_ratings
has_many :roles
end
And there will be another model for Role:
class Role < ActiveRecord::Base
has_many :users
end
If you are using gems like Devise for authentication, it will be much easy.