I have the following models:
User, ProcessType and Remark, as follows:
class User < ActiveRecord::Base
has_many :process_type
end
class Remark < ActiveRecord::Base
belongs_to :process_type
end
class ProcessType < ActiveRecord::Base
belongs_to :user
has_many :remarks
end
only some Users are associated with a ProcessType. When a Remark is added, it gets associated with a certain ProcessType (and each ProcessType has a User responsible). I want that when the User associated with a certain ProcessType logs in, to see all Remarks of that processType.
I am not able to figure out the correct approach, maybe someone could help me.
Thanks!
in User.rb, you can have association with remarks directly since user is associated with process_type and process_type is associated with remarks
has_many :remarks, :through => :process_type
Then to see all remarks you can write this ActiveRecord query
current_user.remarks
Related
I have the following schema for my Rails app :
A project has many reviews, each of those reviews is filled out by a unique User to calculate a global score.
We could say that "An Organization of Users handles many Entities that have many Projects which are reviewed by the Users".
As you can see, I have a circular reference since I linked the tables "Users" & "Reviews".
After many tries and many search, I just can't manage to associate a User to a Review...Here is what I did so far:
1. Creation of an association table "UserReviews"
2. Model User
class User < ApplicationRecord
belongs_to :organization
####
has_many :user_reviews
has_many :reviews, through: :user_reviews
end
3. Model Review
class Review < ApplicationRecord
belongs_to :project
##
belongs_to :user_reviews
has_one :user, through: :user_reviews
end
4. Model UserReview
class UserReview < ApplicationRecord
belongs_to :user
belongs_to :review
end
I want to be able to get user.reviews or review.user.
Basically...I have to admit I'm lost despite the documentation. I never had to deal with this kind of issue.
Many thanks for your help!
Why do you need UserReview model here? I suppose Review model suffices your use case.
Change the Review model to:
class Review < ApplicationRecord
belongs_to :project
belongs_to :user
end
I'm working on a Rails 5.2 project that stores and manages guest lists. I have Event and Guest models, and they are associated with a HABTM relationship. This works well, but there is a requirement to be able to optionally store a grouping of two guests (i.e: couples) and when adding guests to the guest list, the couple can be selected and added together, without the user having to remember which of the individual guests should be added to the guest list together, for example, when selecting guests to be added to a guest list, a user should be able to select "Sam", "Andrew", "Mary & Joseph".
What would be the best way of achieving this in ActiveRecord?
class Event < ApplicationRecord
has_and_belongs_to_many :guests
end
class Guest < ApplicationRecord
has_and_belongs_to_many :events
end
Any help would be much appreciated!
Thanks
You need the following models, untested but you'll get the idea.
class Event < ApplicationRecord
has_many :event_guests
end
class Guest < ApplicationRecord
has_many :event_guests, :as => :assignable
has_many :guest_couples
end
class Couple < ApplicationRecord
has_many :event_guests, :as => :assignable
has_many :guest_couples
end
# table to relate events to either a Guest or a Couple (polymorhpic)
class EventGuest < ApplicationRecord
belongs_to :event
belongs_to :assignable, polymorphic: true
end
# Model to create couples (one-to-many)
class GuestCouple < ApplicationRecord
belongs_to :guest
belongs_to :couple
end
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 a user model, farmer model, doctor model, and education model.
A farmer has a user and many educations.
A doctor has a user and many educations.
How do I setup the database for the education model?
Should it have a farmer_id AND a doctor_id?
But a education cannot belong to a farmer AND and doctor at the same time. It's one or the other.
So my education database entry would either have a farmer_id OR a doctor_id filled in, but not both.
Is there a way to guarantee that only one of the ids could be filled in at a time?
Or is there a better way to associate these models?
Your help would be appreciated!
Oh, and don't worry about the names of the models (farmer, doctor, etc.). It's just an example.
I see two possible solutions for this scenario.
The first one is to make use of polymorphic associations for education. That could look like this:
class Farmer < ActiveRecord::Base
belongs_to :user
has_many :educations, :as => :profession
end
class Doctor < ActiveRecord::Base
belongs_to :user
has_many :educations, :as => :profession
end
class Education < ActiveRecord::Base
belongs_to :profession, :polymorphic => true
end
So instead of education having a doctor_id or a farmer_id it has one profession_id and one profession_type.
The second solution would be to make use of Single Table Inheritance. And in your scenrio, that could be accomplished by letting a Doctor be a User instead of belonging to a User. And of course the same thing for a Farmer. That could look like this:
class User < ActiveRecord::Base
has_many :educations
end
class Farmer < User
end
class Doctor < User
end
class Education < ActiveRecord::Base
belongs_to :user
end
And in this scenario you would add a type column to the User model to store what type of class it is and then only having a user_id in the Education model
I think its appropriate to have the relations this way based on roles.
Class User
has_one :role
has_many :educations
end
Class Role
#What ever roles you have.
#Farmer or Doctor
belongs_to :user
end
class Education
belongs_to :user
end
This way you will store the user_id in the education object, which solves your problem.
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