How to Set Up Specific Active Record Association - ruby-on-rails

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

Related

Rails - Owner of data - User, or company (or group)

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

How to make a resource that belongs_to multiple users in Rails

I'm setting up an app that has Users and Brands. Most users will not be associated with a brand, and will only be able to comment on Brand pages. Some users, however, will be associated with a single brand. I want these users to be the "admins" or owners of this brand. E.g. Users A and B are both "admins" of a brand, and so can create/edit/update the brand, etc. My question is, how should I set up the Brand resource such that it "belongs_to" multiple users? I understand that I could say brands have_many users, but is it weird to say that an object "has" a user? Is it more appropriate to "belong" to users? This description leads me to believe so: "For example, it makes more sense to say that a supplier owns an account than that an account owns a supplier."
It's definitely a has_many relationship.
But it may be clearer to refer to those special users as 'administrators' or 'admins'
class Brand
has_many :administrators, class_name: 'User'
end
If it turns out that a user can be administrator for several brands, then you'll need a join table, either HABTM or HMT. HMT is the better choice in case you want to store characteristics about the join (e.g. when he became administrator)
class BrandUser
belongs_to :user
belongs_to :brand
end
class Brand
has_many :brand_users
has_many :administrators, through: :brand_users, source: :user
end
class User
has_many :brand_users
has_many :brands, through: :brand_users
end

Active Admin Querying on Association Tables Column presence and on its value

I want to active admin filters on an admin panel page based on its column presence.
The model association is as follows:
class User < ActiveRecord::Base
has_many :objects, through: :orders
has_many :orders
end
class Order < ActiveRecord::Base
belongs_to :user
belongs_to :object
end
class Object < ActiveRecord::Base
has_many :users, through: :orders
has_many :orders
end
An order has a quantity column in it.
I want to filter on index page of users in my admin pages which is integrated with active admin gem.
Ex: I want to filter as follows:
users with object quantity greater than 5.
I should check for association record presence and check for the quantities greater than 5 and only show that users.
I also need the negation filters as well.
I want to check for the users who don't buy an order.
I tried to use ransack:
For this, I need to add ransack search query for each object records. Is there any intuitive or any way I can achieve this functionality.

ActiveRecord relationship model with Users and Organizations

I'm trying to find out what's the best logical way to model relationship between models.
I have 4 models:
User
Product
SlackTeam
Organization
Here User has many Products, SlackTeams and Organizations, and SlackTeam belongs to User and has one Organization. Organization should belong to User and SlackTeam. Am I logically correct here?
The workflow is following:
Users can log in with SlackTeam (which automatically creates Organization)
other Users from the same slack team will be added to same Organization once they link up their account with Slack
if Users are connected to many SlackTeams (and Organizations) they can filter to see Products from all Organizations they are part of or only from one
Am I missing something?
class User
has_many :users_ogranizations
has_many :organizations, through: :users_organizations
has_many :products, through: :organizations
end
class Product
belongs_to :organization
end
class Organization
has_many :users_ogranizations
has_many :users, through: :users_organizations
has_many :products
end
class UsersOrganization
belongs_to :user
belongs_to :organization
end
# i'd rather use slack profile than team, because Organization and Team
# already connotes the same thing.
class SlackProfile
end
You can handle your user's login however you like, I would prefer a kind of authentication service, though. All products that belongs to the organization is now accessible to the user, you can then filter the products with:
current_user.products.where(organization: Organization.last)

Rails -- whats the proper relationship model

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

Resources