In my rails app I have 2 models Profile and Skill.
Profile has_and_belongs_to_many Skill and can only have one time the same Skill.
Skill has_and_belongs_to_many Profile. If we respect the first relation, it should therefore not have more than once the same Profile.
When I create my join table I have two possibilities:
rails g migration CreateProfilesSkillsJoinTable profiles:uniq skills
or
rails g migration CreateProfilesSkillsJoinTable profiles skills:uniq
The first option will generate
class CreateProfilesSkillsJoinTable < ActiveRecord::Migration[5.1]
def change
create_join_table :profiles, :skills do |t|
t.index [:profile_id, :skill_id], unique: true
# t.index [:skill_id, :profile_id]
end
end
end
The second will generate
class CreateProfilesSkillsJoinTable < ActiveRecord::Migration[5.1]
def change
create_join_table :profiles, :skills do |t|
# t.index [:profile_id, :skill_id]
t.index [:skill_id, :profile_id], unique: true
end
end
end
You want to make the index unique :
add_index :something, [:profile_id, :skill_id], unique: true
First first rule is verified (you can get 1:2 only once). Note that even with an habtm, you'll tend to create your relation the same way (profile.skills << skill), you just need to ensure skill.profiles << profile does not creates unwanted relations
I am using rails 4.2, I just want to know if there would be any difference if I use the :foreign_key keyword in my migrations rather than just adding a user_id column to add relationship to my models ?
YES
The key difference is not on the application layer but on the database layer - foreign keys are used to make the database enforce referential integrity.
Lets look at an example:
class User < ActiveRecord::Base
has_many :things
end
class Thing < ActiveRecord::Base
belongs_to :user
end
If we declare things.user_id without a foreign key:
class CreateThings < ActiveRecord::Migration
def change
create_table :things do |t|
t.integer :user_id
t.timestamps null: false
end
end
end
ActiveRecord will happily allow us to orphan rows on the things table:
user = User.create(name: 'Max')
thing = user.things.create
user.destroy
thing.user.name # Boom! - 'undefined method :name for NilClass'
While if we had a foreign key the database would not allow us to destroy user since it leaves an orphaned record.
class CreateThings < ActiveRecord::Migration
def change
create_table :things do |t|
t.belongs_to :user, index: true, foreign_key: true
t.timestamps null: false
end
end
end
user = User.create(name: 'Max')
thing = user.things.create
user.destroy # DB says hell no
While you can simply regulate this with callbacks having the DB enforce referential integrity is usually a good idea.
# using a callback to remove associated records first
class User < ActiveRecord::Base
has_many :things, dependent: :destroy
end
I have two models, sellers and customers.
I want it to be a customer has a seller, many customer can have the same seller.
Ideally i want can do customer.seller = seller
With belongs_to association a seller can belongs to jsute one customer.
I use has_and_belongs_to_many association though a in my cas un customer can only have one seller.
# migration
create_table :sellers do |t|
t.string :name
t.timestamps null: false
end
create_table :customers do |t|
t.string :name
t.timestamps null: false
end
create_table :customers_sellers, id: false do |t|
t.belongs_to :customer, index: true
t.belongs_to :seller, index: true
end
# models/seller.rb
class Seller < ActiveRecord::Base
has_and_belongs_to_many :customers
end
# models/customer.rb
class Customer < ActiveRecord::Base
has_and_belongs_to_many :sellers
end
With that i can't do something like that:
customer = Customer.create(name: "John")
seller = Seller.create(name: "Dave")
customer.sellers = seller
I have an error
NoMethodError: undefined method `each' for #<Seller:0x0000000582fb18>
But i can:
customer.sellers<< seller
But if I change the name of the seller like that
Seller.first.name = "Bud"
I want it's to be also modified in my customer.sellers.name.
It's possible to make something like that?
Ok, so for a start, Seller.first.name = "Bud" does nothing to update the database, that name attribute is set on the Seller.first instance which is then lost because you've not assigned it to any variable.
So you'll need to change that to either:
Seller.first.update name: "Bud"
In order to update the DB with the new value, or (more likely) something like:
seller = Seller.first
seller.name = "Bud"
seller.save
That's step 1, actually getting that value saved into the database.
The second problem is that if you have already read customer.sellers from the database then your application already has the value for each of the names stored in memory, you need to reload at least that first record from the DB in order to get the new value:
customer.sellers.reload
And now (I'm assuming that Seller.first is also customer.sellers.first) customer.sellers.first.name will be "Bud"
I have a table called customers. These table has two addresses . One address of work and One direccion of house
Those 2 addresses belong to a table called addresses
I don't know how to relation those 2 tables
Migrations
class CreateCustomers < ActiveRecord::Migration
def change
create_table :customers do |t|
t.string :name
t.integer :address_id #Address of work
t.integer :address_id_1 #Address of home
t.timestamps
end
end
end
class CreateAdresses < ActiveRecord::Migration
def change
create_table :adresses do |t|
t.string :street
t.timestamps
end
end
end
I do not believe this is a good approach or database design. If you want to proceed this way and not get out of the rails convention just create two columns address_id and address_two_id
and in customer.rb
belongs_to :address, class_name: "Address"
belongs_to :address_two, class_name: "Address"
By default rails takes the name of the foreign key and stores it in a column called "name"+"_id"
The better way is two have a column customer_id in your Address model and create a relation in your customer class
customer.rb
has_many :addresses
And you can also validate that a customer has no more than two addresses by adding this validation to
address.rb
validate :validate_two_addresses
def validate_two_addresses
address_count = Address.where(customer_id: self.customer_id).count
errors.add(:base, "You cannot have more than 2 addresses.") unless address_count < 3
end
What I need is a migration to apply unique constraint to a combination of columns. i.e. for a people table, a combination of first_name, last_Name and Dob should be unique.
add_index :people, [:firstname, :lastname, :dob], unique: true
According to howmanyofme.com, "There are 46,427 people named John Smith" in the United States alone. That's about 127 years of days. As this is well over the average lifespan of a human being, this means that a DOB clash is mathematically certain.
All I'm saying is that that particular combination of unique fields could lead to extreme user/customer frustration in future.
Consider something that's actually unique, like a national identification number, if appropriate.
(I realise I'm very late to the party with this one, but it could help future readers.)
You may want to add a constraint without an index. This will depend on what database you're using. Below is sample migration code for Postgres. (tracking_number, carrier) is a list of the columns you want to use for the constraint.
class AddUniqeConstraintToShipments < ActiveRecord::Migration
def up
execute <<-SQL
alter table shipments
add constraint shipment_tracking_number unique (tracking_number, carrier);
SQL
end
def down
execute <<-SQL
alter table shipments
drop constraint if exists shipment_tracking_number;
SQL
end
end
There are different constraints you can add. Read the docs
For completeness sake, and to avoid confusion here are 3 ways of doing the same thing:
Adding a named unique constraint to a combination of columns in Rails 5.2+
Let's say we have Locations table that belongs to an advertiser and has column reference_code and you only want 1 reference code per advertiser. so you want to add a unique constraint to a combination of columns and name it.
Do:
rails g migration AddUniquenessConstraintToLocations
And make your migration look either something like this one liner:
class AddUniquenessConstraintToLocations < ActiveRecord::Migration[5.2]
def change
add_index :locations, [:reference_code, :advertiser_id], unique: true, name: 'uniq_reference_code_per_advertiser'
end
end
OR this block version.
class AddUniquenessConstraintToLocations < ActiveRecord::Migration[5.2]
def change
change_table :locations do |t|
t.index ['reference_code', 'advertiser_id'], name: 'uniq_reference_code_per_advertiser', unique: true
end
end
end
OR this raw SQL version
class AddUniquenessConstraintToLocations < ActiveRecord::Migration[5.2]
def change
execute <<-SQL
ALTER TABLE locations
ADD CONSTRAINT uniq_reference_code_per_advertiser UNIQUE (reference_code, advertiser_id);
SQL
end
end
Any of these will have the same result, check your schema.rb
Hi You may add unique index in your migration to the columns for example
add_index(:accounts, [:branch_id, :party_id], :unique => true)
or separate unique indexes for each column
In the typical example of a join table between users and posts:
create_table :users
create_table :posts
create_table :ownerships do |t|
t.belongs_to :user, foreign_key: true, null: false
t.belongs_to :post, foreign_key: true, null: false
end
add_index :ownerships, [:user_id, :post_id], unique: true
Trying to create two similar records will throw a database error (Postgres in my case):
ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_ownerships_on_user_id_and_post_id"
DETAIL: Key (user_id, post_id)=(1, 1) already exists.
: INSERT INTO "ownerships" ("user_id", "post_id") VALUES ($1, $2) RETURNING "id"
e.g. doing that:
Ownership.create!(user_id: user_id, post_id: post_id)
Ownership.create!(user_id: user_id, post_id: post_id)
Fully runnable example: https://gist.github.com/Dorian/9d641ca78dad8eb64736173614d97ced
db/schema.rb generated: https://gist.github.com/Dorian/a8449287fa62b88463f48da986c1744a
If you are creating a new table just add unique: true
class CreatePosts < ActiveRecord::Migration[6.0]
def change
create_table :posts do |t|
t.string :title, unique: true
t.text :body
t.references :user, foreign_key: true
t.timestamps
end
add_index :posts, :user_id, unique: true
end
end