There are models: member(id,project_id,user_id), project(id), user(id), somemodel(id, project_id, user_id).
member belongs_to :project, belongs_to :user
somemodel belongs_to :project, belongs_to :user
I would like that when a member is deleted so somemodel (that has project_id and user_id the same as that member) is deleted also. How to do that?
To do that I would like has_many :somemodels, dependent: :destroy to be added to member , but I don't know the right parameters to give to has_many :somemodels in member class. has_many :somemodels, dependent: :destroy alone doesn't work as it tries to search for somemodel by member_id which is not present on the somemodels table and so the no column error occurs.
What would be the right has_many :somemodels ... to be added to Member?
An alternative way could be to create a :through association for this specific relation, and then set that one to dependent: :destroy. I think it would be structured like this:
class Member < ActiveRecord::Base
belongs_to :user
belongs_to :project
has_many :some_models, ->(member) { where(user_id: member.user_id) },
through: :project,
dependent: :destroy
end
I haven't tested it, but it seems right as far as I can see.
I think you should add a after_destroy callback to Member model:
# In member model
class Member < ApplicationRecord
after_destroy :also_destroy_somemodel
...
def also_destroy_somemodel
# Fast, use in case somemodel dont have any callbacks
Somemodel.where(project_id: project_id, user_id: user_id).delete_all
# Or
# Slow, use in case somemodel has callback(s)
# Somemodel.where(project_id: project_id, user_id: user_id).each &:destroy
end
end
Hope it helps.
Related
I have an issue with mongoid / rails relations, I know that there are a lot of topics with this kind of issue, but I don't find any that help me ..
I have these models :
class Project
include Mongoid::Document
belongs_to :owner, :class_name => 'User', inverse_of: :projects
has_many :members
end
class Member
include Mongoid::Document
belongs_to :project, inverse_of: :members
belongs_to :user
end
class User
include Mongoid::Document
has_many :projects, inverse_of: :user
end
When I try to record an user as a member, I have this error :
Mongoid::Errors::InverseNotFound (
message:
When adding a(n) User to Project#members, Mongoid could not determine the inverse foreign key to set. The attempted key was 'project_id'.
summary:
When adding a document to a relation, Mongoid attempts to link the newly added document to the base of the relation in memory, as well as set the foreign key to link them on the database side. In this case Mongoid could not determine what the inverse foreign key was.
resolution:
If an inverse is not required, like a belongs_to or has_and_belongs_to_many, ensure that :inverse_of => nil is set on the relation. If the inverse is needed, most likely the inverse cannot be figured out from the names of the relations and you will need to explicitly tell Mongoid on the relation what the inverse is.
Example:
class Lush
include Mongoid::Document
has_one :whiskey, class_name: "Drink", inverse_of: :alcoholic
end
class Drink
include Mongoid::Document
belongs_to :alcoholic, class_name: "Lush", inverse_of: :whiskey
end):
I don't understand why, I think something is wrong with the relations, and the inverses relation but I don't know how to fix this issue.
class Project
include Mongoid::Document
belongs_to :owner, :class_name => 'User', inverse_of: :projects
has_many :members
has_many :users
end
But I don't think your modeling actually will accomplish what you want. In a relational database you would use an indirect association with a join table:
class User < ActiveRecord::Base
has_many :memberships
has_many :projects, through: :memberships
end
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :project
end
class Project < ActiveRecord::Base
has_many :memberships
has_many :members, through: :memberships,
source: :user
end
But over in Mongoland there are no joins so we we need to use a different approach which is embedding:
class Project
# ...
embeds_many :users
end
class User
# ...
embedded_in :project
end
Or you can fake an indirect association if you need to be able to add data to the intermediate model:
class Project
# ...
embeds_many :memberships
def members
Patient.in(id: memberships.pluck(:user_id))
end
end
class Membership
# ...
field :approved, type: Boolean
belongs_to :user
embedded_in :project
end
class User
# ...
def projects
Project.where("members.user_id" => id).all
end
end
I am trying to scope through an array of child records based on a value in one of the parent columns. I am trying to find all the ShoppingCartItems that belong to a Product with a category "Bundle."
I am trying to use acts_as_shopping_cart_gem
My Models.
User.rb
class User < ActiveRecord::Base
has_many :shopping_carts, dependent: :destroy
end
ShoppingCart.rb
class ShoppingCart < ActiveRecord::Base
acts_as_shopping_cart_using :shopping_cart_item
belongs_to :user
has_many :shopping_cart_items, dependent: :destroy
has_many :products, through: :shopping_cart_items
end
Product.rb
class Product < ActiveRecord::Base
has_many :shopping_cart_items, dependent: :destroy
end
ShoppingCartItem.rb
class ShoppingCartItem < ActiveRecord::Base
belongs_to :product, dependent: :destroy
scope :bundles, -> {
joins(:product).where('products.category = ?', 'Bundles') unless category.blank?
}
end
I am getting this error:
> undefined local variable or method `category' for
> #<Class:0x007fc0f40310d0>
Your problem is actually straight forward - there is nowhere you defined the category variable.
This is how I would do that (generalized scope):
scope :by_category, lambda { |category|
joins(:product).where(products: { category: category })
}
Note, there is no unless statement - if the category is not passed to scope, it will raise the ArgumentError.
Then use the scope for any category:
ShoppingCartItem.by_category('Bundles')
To prevent the blank category to be passed into scope, just make sure you pass the right string. You can create a dropdown of categories:
Product.pluck(:category)
or something similar, if it is a part of user interface.
The category field on your scope regards the ShoppingCartItem? If so, try self.category.blank?. If not, just remove the unless statement.
Maybe you need to add Category model and add this relation:
class Product < ActiveRecord::Base
has_many :shopping_cart_items, dependent: :destroy
belongs_to :category
end
If we have 3 models => Customer, User and Thing and another model Owner thats inherits from User and we try create a has_many through association like this:
class Customer < ActiveRecord::Base
has_many :things, :dependent => :destroy
has_many :owners, through: :things
end
class Thing < ActiveRecord::Base
belongs_to :customer, foreign_key: "customer_id"
belongs_to :owner, foreign_key: "owner_id"
end
class Owner < User
has_many :things, :dependent => :destroy
has_many :customers, through: :things
end
Why will #owner.things not work for us? (#owner is an instance of Owner). It gives undefined method "things" error.
#owner is the current_user, but how do you specify it to be an instance of User?
Is the only solution to change owner_id to user_id or is there a better solution, please?
As you state, current_user is an instance of User, not of its subclass, Owner.
If you want to add that relation to the current_user, you could add it to its class, User, instead of Owner:
class User < ActiveRecord::Base # or whatever superclass you have for User
has_many :things, dependent: :destroy
end
Otherwise, if you want to stick to Owner, you should overwrite the creation of current_user so that it uses the Owner class instead of User.
I've been going back and forward on this and I would like some advices.
I have "User" that can be part of many "Organizations", and for each one they can have many "Roles". (actually I have this scenario repeated with other kind of users and with something like roles, but for the sake of the example I summed it up).
My initial approach was doing a Table with user_id, organization_id and role_id, but that would mean many registers with the same user_id and organization_id just to change the role_id.
So I thought of doing an organization_users relation table and an organization_users_roles relation. The thing is, now I don't exactly know how to code the models.
class Organization < ActiveRecord::Base
has_and_belongs_to_many :users, join_table: :organization_users
end
class User < ActiveRecord::Base
has_and_belongs_to_many :organizations, join_table: :organization_users
end
class OrganizationUser < ActiveRecord::Base
has_and_belongs_to_many :users
has_and_belongs_to_many :organizations
has_many :organization_user_roles
has_many :roles, through: :organization_user_roles
end
class OrganizationUserRole < ActiveRecord::Base
has_and_belongs_to_many :roles
has_and_belongs_to_many :organization_users
end
class Role < ActiveRecord::Base
has_and_belongs_to_many :organization_user_roles
end
If for example I want to get: ´OrganizationUser.first.roles´ I get an error saying: PG::UndefinedTable: ERROR: relation "organization_user_roles" does not exist
How should I fix my models?
You should use a much simpler approach. According to your description, Roles is actually what connects Users to Organizations and vice-versa.
Using the has_many and has_many :through associations, this can be implemented like the following:
class User < ActiveRecord::Base
has_many :roles, inverse_of: :users, dependent: :destroy
has_many :organizations, inverse_of: :users, through: :roles
end
class Organization < ActiveRecord::Base
has_many :roles, inverse_of: :organizations, dependent: :destroy
has_many :users, inverse_of: :organizations, through: :roles
end
class Role < ActiveRecord::Base
belongs_to :user, inverse_of: :roles
belongs_to :organization, inverse_of: :roles
end
If you wish to preserve roles when you destroy users or organizations, change the dependent: keys to :nullify. This might be a good idea if you add other descriptive data in your Role and want the role to remain even though temporarily vacated by a user, for example.
The has_many :through association reference:
http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
To add to jaxx's answer (I upvoted), I originally thought you'd be best looking at has_many :through:
#app/models/user.rb
class User < ActiveRecord::Base
has_many :positions
has_many :organizations, through: :positions
end
#app/models/position.rb
class Position < ActiveRecord::Base
#columns id | user_id | organization_id | role_id | etc | created_at | updated_at
belongs_to :user
belongs_to :organization
belongs_to :role
delegate :name, to: :role #-> #position.name
end
#app/models/organization.rb
class Organization < ActiveRecord::Base
has_many :positions
has_many :users, through: :positions
end
#app/models/role.rb
class Role < ActiveRecord::Base
has_many :positions
end
This will allow you to call the following:
#organization = Organization.find x
#organization.positions
#organization.users
#user = User.find x
#user.organizations
#user.positions
This is much simpler than your approach, and therefore has much more ability to keep your system flexible & extensible.
If you want to scope your #organizations, you should be able to do so, and still call the users / positions you need.
One of the added benefits of the code above is that the Position model will give you an actual set of data which can be shared between organizations and users.
It resolves one of the main issues with jaxx's answer, which is that you have to set a role for every association you make. With my interpretation, your roles can be set on their own, and each position assigned the privileges each role provides.
If the user can have many Roles for a single organisation,
and OrganizationUser represents this membership,
than, yes, you need another table for organization_user_roles.
You need to explicitly create it in the database (normally with a migration)
To not get confused, try to find a nice name for OrganisationUser, like employment, membership, etc.
How can I delete nested objects in a form? I found out that I need to add :allow_destroy in the parent model at the accepts_nested_attributes_for directive.
Further, I want to restrict the deletion. A nested object only should be deleted, if the parent object is the only one that retains the association.
Example:
class Internship < ActiveRecord::Base
belongs_to :company
accepts_nested_attributes_for :company, allow_destroy => true
end
class Company < ActiveRecord::Base
has_many :internships
end
Explanation: A company can host many internships. Therefore, I do not want to delete the company record as long as there is at least one other internship associated with it.
You could use dependent => :destroy
class Internship < ActiveRecord::Base
belongs_to :company
accepts_nested_attributes_for :company, allow_destroy => true
end
class Company < ActiveRecord::Base
has_many :internships, :dependent => :destroy
end
If you return false in a before_destroy filter, then the destroy action will be blocked. So we can check to see if there are any internships associated to the company, and block it if so. This is done in the company model.
class Company < ActiveRecord::Base
has_many :internships
before_destroy :ensure_no_internships
private
def ensure_no_internships
return false if self.internships.count > 0
end
end