I have a table Atribuition with 2 references from User table.
class Attribuition < ApplicationRecord
belongs_to :user, class_name: 'User', foreign_key: 'user_id'
belongs_to :not_rated, class_name: 'User', foreign_key: 'not_rated_id'
end
The User model:
class User < ApplicationRecord
has_many :attribuitions, dependent: :destroy
end
When i destroy an user marked in not_rated i want it to be destroyed, but it just happens when i destroied an user marked as user_id, then the attribute row is deleted. I wanna make dependent:: destroy to work for many references of same model. That is possible?
My migration is:
class CreateAttribuitions < ActiveRecord::Migration[5.2]
def change
create_table :attribuitions do |t|
t.references :user
t.references :not_rated, index: { unique: true }
t.timestamps
end
end
end
Edit:
First you do following change as rails use convention over configuration
class Attribuition < ApplicationRecord
- belongs_to :user, class_name: 'User', foreign_key: 'user_id'
+ belongs_to :user
end
Changes needed
When you mention has_many :attribuitions, dependent: :destroy by side of User model class_name will be Attribuition and foreign_key will be user_id stored in attributions table.
So if you need to destroy attribuitions related by foreign_key not_rated_id & user_id then you need following changes.
class User < ApplicationRecord
has_many :attribuitions, dependent: :destroy # default foreign_key is user_id
has_many :not_rated_attribuitions, foreign_key: 'not_rated_id', dependent: :destroy
end
Related
I'm trying to create a Referral program on a Rails app and I struggle with the relationships.
My Referral model is pretty simple : godfather_id, godson_id, state
Both godfather and godson ids references an User, which can have many godsons but only one godfather.
class Referral < ApplicationRecord
belongs_to :user
belongs_to :godson, class_name: 'User'
end
The issue comes in my User model. I wan't to be able to do user.godsons to get an array of godsons Users and user.godfather to get the godfather User.
I tried a few things and I think those two where the closest to what I need to do (User model simplified for the example).
class User < ApplicationRecord
has_many :referrals
has_many :godson, -> { where(godfather_id: id) }, through: :referrals
has_one :godfather, -> { where(godson_id: id) }, through: :referrals
end
class User < ApplicationRecord
has_many :godson_relations, class_name: 'Referral', foreign_key: 'godson_id'
has_many :godsons, through: :godson_relations
has_one :godfather_relation, class_name: 'Referral', foreign_key: 'godfather_id'
has_one :godfather, through: :godfather_relations
end
I'm really unsure about how to materialize this relationship, any help will be appreciated 🙏
To make an actual self-referential assocation you would just add a column on the users table that points back to the same table:
class AddGodfatherToUsers < ActiveRecord::Migration[6.1]
def change
add_reference :users, :godfather, null: true,
foreign_key: { to_table: :users }
end end
class User
belongs_to :god_father,
class_name: 'User',
optional: true,
inverse_of: :god_children
has_many :god_children,
class_name: 'User',
foreign_key: :god_father_id
inverse_of: :god_father
end
If you must store Referalls as a separate table you were kind of on the right track but you got the foreign keys backwards:
class Referral < ApplicationRecord
# you better be explicit here or its going to get extremely confusing
belongs_to :godfather, class_name: 'User'
belongs_to :godson, class_name: 'User'
end
class User < ApplicationRecord
has_many :referrals_as_godfather,
class_name: 'Referral', foreign_key: 'godfather_id'
has_one :referral_as_godson,
class_name: 'Referral',
foreign_key: 'godfather_id'
has_many :godsons, through: :referrals_as_godfather
has_one :godfather, through: :referral_as_godson
end
It should be noted that has_one in no way guarentees that a user can have only one referral (and thus one godfather). It just adds a LIMIT 1 to the query. You would have to enforce that with a uniqueness constraint and validations.
I have a user instance that has many invitees but only one inviter.
I am trying to access the inviter instance associated with that user and also his invitees.
i.e:
user.inviter #=> return another user instance.
user.invitees #=> return a collection on user instances
User.rb
class User < ActiveRecord::Base
has_one :inviter, class_name: Invitation, foreign_key: :invitee_id
has_many :invitees, class_name: Invitation, foreign_key: :inviter_id
end
Invitation.rb
class Invitation < ActiveRecord::Base
belongs_to :inviter, class_name: User, foreign_key: :inviter_id
belongs_to :invitee, class_name: User, foreign_key: :invitee_id
end
migration
class CreateInvitations < ActiveRecord::Migration
def change
create_table :invitations do |t|
t.references :inviter, references: :user, index: true
t.references :invitee, references: :user, index: true
t.foreign_key :users, column: :inviter_id
t.foreign_key :users, column: :invitee_id
t.timestamps
end
end
end
This works half of the way because if I call user.inviter on a user that has an inviter it will return the invitation instance but not the user like I would like. Same for user.invitees returns a collection on invitation instances.
Do y'all have an idea of how to make it work ?
Your should use through option like this:
class User < ActiveRecord::Base
has_one :invitation, inverse_of: :inviter
has_one :inviter, through: :invitation
has_many :invitations, inverse_of: :invitee
has_many :invitees, through: :invitations
end
class Invitation < ActiveRecord::Base
belongs_to :inviter, class_name: User, inverse_of: :invitation
belongs_to :invitee, class_name: User, inverse_of: :invitations
end
user.invitees will give collection of invitation records.
Using IN query into User model with all invitee_id which reference to user model will give you collection of users.
user_ids = user.invitees.map(&:invitee_id)
User.where(id: user_ids)
i have two class User and Bug there are two foreign keys in bug which are referencing to user_id ..the problem is that how i store user_id in foreign key column while creating the record.like for example if user enter bug then his id store in buger_id colunm.
class Bug
belongs_to :buger, class_name: "User", foreign_key: "buger_id"
belongs_to :developer , class_name: "User", foreign_key: "developer_id"
class user
has_many :created_bugs, class_name:"bugs"
has_many :developed_bugs, class_name:"bugs"
You need to add the foreign_key to the has_many declaration!
class User < ActiveRecord::Base
has_many :created_bugs, class_name: 'Bug' , foreign_key: :buger_id
has_many :developed_bugs, class_name: 'Bug' , foreign_key: :developer_id
end
class Bug < ActiveRecord::Base
belongs_to :buger, class_name: 'User'
belongs_to :developer , class_name: 'User'
end
See also: http://guides.rubyonrails.org/association_basics.html
You can specify class and foreign key on the has_many line as well.
has_many :created_bugs, class_name:"Bug", foreign_key: 'buger_id'
has_many :developed_bugs, class_name:"Bug", foreign_key: 'developer_id'
In Rails 5.1 or greater you can do it like this:
Migration
class CreateBug < ActiveRecord::Migration
def change
ccreate_table(:bugs) do |t|
t.references :bugger, foreign_key: { to_table: 'users' }
t.references :developer, foreign_key: { to_table: 'users' }
end
end
end
This will create the fields bugger_id, and developer_id and make the database level references to the users table
Models
class Bug < ActiveRecord::Base
belongs_to :bugger, class_name: "User"
belongs_to :developer, class_name: "User"
end
class User < ActiveRecord::Base
has_many :created_bugs, class_name: "Bug", foreign_key: "bugger_id"
has_many :developed_bugs, class_name: "Bug", foreign_key: "developer_id"
end
FactoryBot
If you use FactoryBot then your factory might look something like this:
FactoryBot.define do
factory :bug do
association :bugger, factory: :user
association :developer, factory: :user
end
end
I have a problem related with this association. A pasted code is better than any title:
table.rb
class Table < ActiveRecord::Base
has_and_belongs_to_many :clients, class_name: 'User'
has_and_belongs_to_many :managers, class_name: 'User'
end
user.rb
class User < ActiveRecord::Base
has_and_belongs_to_many :tables
end
migration - join table
class UsersToTable < ActiveRecord::Migration
def change
create_table :tables_users, id: false do |t|
t.references :user, as: :client
t.references :user, as: :manager
t.references :table
end
end
end
Problem
tab = Table.new
tab.save
tab.clients.create
tab.clients.create
tab.clients.create
tab.managers.create
tab.managers.size # == 4
tab.clients.size # == 4
When I creating associated Objects(Users) they all are linked to both clients and managers.
I want to be able to create them separately - When creating a client - only number of clients rise, when creating manager, only number of managers rise.
In other words I want this:
tab.managers.size # == 1
tab.clients.size # == 3
Could you please help?
has_and_belongs_to_many :stuff, class_name: 'StuffClass' is just DSL for:
has_many "<inferred_join_table_name>"
has_many :stuff, through: "<inferred_join_table_name>"
It seems that since clients and managers are names for Users, the inferred join table get's to be "TablesUsers", and that is not right.
Try specifyng the join table for both and using different join tables for each relationship:
class Table
has_many :tables_clients
has_many :clients, through: :tables_clients
has_many :tables_managers
has_many :clients, through: :tables_managers
end
class TablesClients
belongs_to :client, class_name: 'User'
belongs_to :table
end
create_table :tables_clients, id: false do |t|
t.references :client, index: true
t.references :table, index: true
end
# and the same for tables_managers
Then the user belongs to Tables in too different ways:
class User
has_many :client_tables_users, class_name: 'TablesUsers', foreign_key: :client_id
has_many :tables_as_client, through: :client_tables_users, source: :table
has_many :managed_tables_users, class_name: 'TablesUsers', foreign_key: :manager_id
has_many :managed_tables, through: :managed_tables_users, source: :table
end
I'm trying to create an app to share or give products. So I have two models : User and Product.
A user can have many products, as an owner or as a borrower. A product has only one owner and only one borrower.
First I did something like that :
> rails generate model User name:string
class User
has_many :owned_products, class_name: "Product", foreign_key: "owner_id"
has_many :borrowed_products, class_name: "Product", foreign_key: "borrower_id"
end
> rails generate model Product name:string owner_id:integer borrower_id:integer
class Product
belongs_to :owner, class_name: "User", foreign_key: "owner_id"
belongs_to :borrower, class_name: "User", foreign_key: "borrower_id"
end
I added in my Product controller a security filter that enable the update method only for product's owner. But when I want to change the product's borrower, I have some kind of a problem, because the borrower is never the owner, so the product can not be updated.
So now I'm wondering if I should not take the foreign_key out of my products model, in order to dissociate the update action of a user on his own product, and the update action of a user to borrow a product that don't belongs to him...
> rails generate model User name:string
class User
has_many :properties
has_many :loans
has_many :owned_products, through: :properties
has_many :borrowed_products, through: :loans
end
> rails generate model Property owner_id:integer owned_product_id:integer
class Property
belongs_to :owner, class_name: "User", foreign_key: "user_id"
belongs_to :owned_product, class_name: "Product", foreign_key: "product_id"
end
> rails generate model Loan borrower_id:integer borrowed_product_id:integer
class Loan
belongs_to :borrower, class_name: "User", foreign_key: "user_id"
belongs_to :borrowed_product, class_name: "Product", foreign_key: "product_id"
end
> rails generate model Product name:string
class Product
has_one :property
has_one :loan
has_one :owner, through: :property
has_one :borrower, through: :loan
end
What do you think about it ?
Since borrowed products and owned products are the same type of object with the same list of attributes, but differ only in behavior, I would use single table inheritance for Product.
Migrations:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
# ...
t.timestamps
end
end
end
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.integer :ownerable_id
t.string :ownerable_type
# ...
t.timestamps
end
end
end
Models:
class User < ActiveRecord::Base
has_many :products, :as => :ownerable
end
class Product < ActiveRecord::Base
belongs_to :user, :polymorphic => true
end
class OwnedProduct < Product
end
class BorrowedProduct < Product
end
The benefit of this approach is that you can just define the appropriate behavior in each model without asking it if it's "owned" or "borrowed." Just tell your models what to do and leave the decisions up to each object to do the right thing.