I've implemented a model with modified_by_id and modifies_id self-referencing attributes to persist "dirty" state of some models.
class CreateModels < ActiveRecord::Migration[5.0]
def change
create_table :models do |t|
t.string :name
t.modified_by_id :integer
t.modifies_id :integer
end
end
end
This double association has the intention of speeding (and simplifying) two queries
the latest version of things where(modified_by_id: nil)
the persisted version of things where(modifies_id: nil)
Everything works well when handling the ids manually, but I can't model it properly with relations.
has_one :modifies, class_name: 'Some::Model', foreign_key: 'modifies_id'
belongs_to :modified_by, class_name: 'Some::Model', foreign_key: 'modified_by_id'
Using it so populates only one of the ids when calling model.modifies = new_model
And this:
has_one :modifies, class_name: 'Some::Model', foreign_key: 'modifies_id'
belongs_to :modified_by, class_name: 'Some::Model', inverse_of: :modifies, foreign_key: 'modified_by_id'
pupulates only one model with the same id twice (modifes_id == modofies_by_id)
How should this be modeled?
Related
I am building a Ruby on Rails 6 application where I have a model Archive, and a another model ArchiveAgencies. In the Archive model I must have a sender agency and a receiver agency, which should represent ArchiveAgencies.
After going through the Rails docs, and some StackOverflow QA:
How do I add migration with multiple references to the same model in one table? Ruby/Rails
Defining two references to the same column in another table
Ruby on Rails: two references with different name to the same model
I came up with this approach:
Models
class Archive < ActiveRecord::Base
belongs_to :sender_agency, class_name: ArchiveAgencies, foreign_key: "sender_agency_id"
belongs_to :receiver_agency, class_name: ArchiveAgencies, foreign_key: "receiver_agency_id"
end
class ArchiveAgency < ActiveRecord::Base
has_many :archives, inverse_of: 'sender_agency'
has_many :archives, inverse_of: 'receiver_agency'
end
Migration
class CreateArchiveAgencies < ActiveRecord::Migration[6.0]
def change
create_table :archives do |t|
t.string :name
t.timestamps
end
end
end
class CreateArchives < ActiveRecord::Migration[6.0]
def change
create_table :archives do |t|
t.references :sender_agency, foreign_key: { to_table: :archive_agencies }
t.references :receiver_agency, foreign_key: { to_table: :archive_agencies }
t.timestamps
end
end
end
Is this the best approach for this case?
Would, having two inverse_of statements in the model ArchiveAgency work?
If you declare multiple associations with the same name the last one will overwrite the others.
Instead you need to use unique names for each association and configure them properly:
class ArchiveAgency < ActiveRecord::Base
has_many :archives_as_sender,
inverse_of: :sender_agency,
foreign_key: :sender_agency_id,
class_name: 'Archive'
has_many :archives_as_receiver,
inverse_of: :receiver_agency,
foreign_key: :receiver_agency_id,
class_name: 'Archive'
end
class Archive < ActiveRecord::Base
belongs_to :sender_agency, # foreign_key can be derived from the name
class_name: 'ArchiveAgency', # needs to be a string - not the class itself
inverse_of: :archives_as_sender
belongs_to :receiver_agency,
class_name: 'ArchiveAgency'
inverse_of: :archives_as_receiver
end
I have two models: person.rb and relationship.rb
I need my :relationships table to reference two rows from the :people table as foreign keys.
Here are the migrations for both tables:
class CreatePeople < ActiveRecord::Migration[5.1]
def change
create_table :people do |t|
t.string :first_name
t.string :second_name
t.integer :age
t.string :gender
t.timestamps
end
end
end
class CreateRelationships < ActiveRecord::Migration[5.1]
def change
create_table :relationships do |t|
t.references :person_a
t.references :person_b
t.string :status
t.timestamps
end
end
end
The idea is the :person_a and :person_b fields will both be individual records from the :people table referenced as foreign keys, while the :status field will just be a description of their relationship ("Married", "Friends", "Separated", etc.)
I'm trying to find out:
1) What is the additional code I have to write in the CreateRelationships migration above in order to set :person_a and :person_b up as foreign keys from the :people table?
2) What code do I need to write in the model files (person.rb and relationship.rb) for both tables below to define the relationship structure I'm talking about?
class Person < ApplicationRecord
end
class Relationship < ApplicationRecord
end
I've found one other question on here that deals with this issue, but the answers given were conflicting, some incomplete, and others working with older versions of Rails. None of them have worked for me.
I'm using Rails 5.1.4
You have defined you migration correctly just add the following in your model to define the relationship between the model.
class Person < ApplicationRecord::Base
has_many :relationships, dependent: :destroy
end
class Relationship < ApplicationRecord::Base
belongs_to :person_a, :class_name => 'Person'
belongs_to :person_b, :class_name => 'Person'
end
This allows you to access the Person that a Relationship belongs to like this:
#relationship.person_a #user assigned as the first person.
#relationship.person_b #user assigned as the second person.
Hope this works.
EDIT: Apologies for a rushed and wrong answer.
Initially, I thought that simple has_many/belongs_to association is possible and sufficient.
class Person < ApplicationRecord
has_many :relationships #dependent: :destroy
end
class Relationship < ApplicationRecord
belongs_to :person_a, class_name: "Person"
belongs_to :person_b, class_name: "Person"
enum status: [:married, :friends, :separated]
end
As #engineersmnky pointed out, has_many association can't work here because there is no person_id column in relationships table. Since we can declare only one custom foreign key in has_many association, it's not possible to declare it here this way. belongs_to will work, but I don't think that's enough.
One way is to skip declaring has_many and stick to custom method for querying relationships:
class Person < ApplicationRecord
def relationships
Relationship.where("person_a_id = ? OR person_b_id = ?", id, id)
end
end
It will give you an ActiveRecord::Relation to work with, containing exactly the records you need. The drawbacks of this solution are numerous - depending on your needs, you will probably need more code for inserting data, starting with a setter method to assign relationships to people...
What could be a real solution, is to have a composite primary key in Relationship model - composed of :person_a_id and :person_b_id. ActiveRecord doesn't support composite primary keys, but this gem seems to fill the gap. Apparently it allows to declare such key and use it as a foreign key in a has_many association. The catch is that your person_a/person_b pairs would have to be unique across relationships table.
I had to do the same for a chat module, this is an example to how you can do it:
class Conversations < ApplicationRecord
belongs_to :sender, foreign_key: :sender_id, class_name: 'User'
belongs_to :recipient, foreign_key: :recipient_id, class_name: 'User'
end
class User < ApplicationRecord
...
has_many :conversations
has_many :senders, through: :conversations, dependent: :destroy
has_many :recipients, through: :conversations, dependent: :destroy
...
end
More explanations at complex-has-many-through
Hope it helps,
You can do like this
In relationship model write
belongs_to : xyz, class_name: "Person", foreign_key: "person_a"
belongs_to : abc, class_name: "Person", foreign_key: "person_b"
In Person model write
has_many :relationships, dependent: :destroy
hope it will help
I am having problems creating this association: Consider a model "Entry". I want entries to have many entries as parents and I want entries to have many entries as children. I want to realize this relation via a model I called "Association", so here is what I tried:
Migration:
class CreateAssociations < ActiveRecord::Migration[5.0]
def change
create_table :associations do |t|
t.integer :parent_id
t.integer :child_id
end
end
end
Association model:
class Association < ApplicationRecord
belongs_to :parent, class_name: 'Entry'
belongs_to :child, class_name: 'Entry'
end
so far it works. But now how do I use this to create two many-to-many relations on a model to itself?
class Entry < ApplicationRecord
# has many parent entries of type entry via table associations and child_id
# has many child entries of type entry via table associations and parent_id
end
This should work:
class Entry < ApplicationRecord
has_and_belongs_to_many :parents, class_name: 'Entry', join_table: :associations, foreign_key: :child_id, association_foreign_key: :parent_id
has_and_belongs_to_many :children, class_name: 'Entry', join_table: :associations, foreign_key: :parent_id, association_foreign_key: :child_id
end
This follows on from my previous question
I have a user model with two self joins, seller and buyer.
I have an categories model, a seller has_and_belongs_to_many categories, as does a buyer.
How do I create the migration so I can do seller.categories and categories.buyers etc...
I thought it would be something like I have below but it doesn't work...
def change
create_table :categories_sellers do |t|
t.references :category
t.references :user
end
add_foreign_key :categories_sellers, :users, column: :trainer_id
add_index :categories_users, [:category_id, :seller_id]
add_index :categories_users, :seller_id
end
end
To answer your question, it looks like you just need to change t.references :user to t.references :seller.
That said, I would highly suggest modeling your project as such:
module User
extend ActiveSupport::Concern
has_many :category_users, as: :user
has_many :categories, through: :category_users
# include any common methods for buyers and sellers,
# basically your current User model
end
class Buyer < ActiveRecord::Base
include User
end
class Seller < ActiveRecord::Base
include User
end
class CategoryUser < ActiveRecord::Base
belongs_to :category
belongs_to :user, polymorphic: true
end
class Category < ActiveRecord::Base
has_many :category_users
has_many :buyers, through: :category_users, source: :user, source_type: 'Buyer'
has_many :sellers, through: :category_users, source: :user, source_type: 'Seller'
end
I know that may require some changes that you didn't anticipate, but in doing that, you get more natural methods such as:
category.buyers
category.sellers
buyer.categories
seller.categories
Under the hood, your join table will have columns like:
id -- the row id
category_id -- the category id, of course
user_id -- the id of the Buyer or Seller
user_type -- one of "Buyer" or "Seller" (or any other type you deem as "user")
To run the migrations:
User doesn't need one, it's not a model.
Buyer and Seller are pretty straightforward, old User model + Buyer/Seller model.
Category doesn't need one as it already exists.
CategoryUser:
def change
create_table :category_users do |t|
t.references :category
t.references :user, polymorphic: true
end
add_index :category_users, :category_id
add_index :category_users, [:category_id, :user_id]
add_index :category_users, [:category_id, :user_id, :user_type]
end
I haven't checked this personally but should be right, or close. The overall principle, though, is to make use of polymorphic associations to make a more natural association between "some kind of user" (whether it be a Buyer, Seller, or any other type of user you come up with) and a category. Then, you don't need to replicate the same sort of associations over and over again because the models slightly vary.
Here's more details on this approach:
http://guides.rubyonrails.org/association_basics.html#polymorphic-associations
I'm looking to set up a self-referencing has_and_belongs_to_many relationship using rails 4, with a postgres db.
Basically I've got a Single Inheritance Table set up called Resource, which holds People, Places, and Things in it. (This works beautifully.)
create_table :resources do |t|
t.string :name
t.string :type
t.text :description
end
I'm trying to create a 'has_and_belongs_to_many' relationship so that each Resource can have a series of 'owners', which will be an Relation of People. Each Person, in turn, will have a series of 'possessions'. Since it's a single table, I'll need to join the Resource table to itself.
My migration for the join table looks like this:
create_table :owners_possessions, id: false do |t|
t.integer :owner_id # the id of a person
t.integer :possession_id # the id of a place/thing owned by that person
end
Person.rb:
class Person < Resource
has_and_belongs_to_many :possessions, class_name: :resources,
join_table: :owners_possessions,
foreign_key: :owner_id,
association_foreign_key: :possession_id
end
Resource.rb:
class Resource < ActiveRecord::Base
has_and_belongs_to_many :owners, class_name: :people,
join_table: :owners_possessions,
association_foreign_key: :possession_id,
foreign_key: :owner_id
end
However, when running Resource.find(x).owners in the console, I get the following error message:
ActiveRecord::StatementInvalid: Could not find table 'resources_resources'
Which is perturbing because everything I've searched so far has pointed toward the join_table option as a way to get it looking at the right table.
Furthermore, running `Person.find(x).possessions' yields
NameError: uninitialized constant Person::Possession
What might I be missing here?
I can't reproduce the errors you posted, I suppose you altered your code somewhat.
Anyway, the class_name option in your associations should be the exact name of the other model. So 'Person' in singular form rather than :people:
class Person < Resource
has_and_belongs_to_many :possessions,
class_name: 'Resource',
join_table: :owners_possessions,
foreign_key: :possession_id,
association_foreign_key: :owner_id
end
class Resource < ActiveRecord::Base
has_and_belongs_to_many :owners,
class_name: 'Person',
join_table: :owners_possessions,
association_foreign_key: :possession_id,
foreign_key: :owner_id
end
Notice I also swapped the :foreign_key and :association_foreign_key values so they return the appropriate records.