M
M
madcore2015-06-05 18:41:52
Ruby on Rails
madcore, 2015-06-05 18:41:52

Link models through the name of one of the models, and not through the id?

There is an Album and a Song model.
How to connect them not through t.references :album - i.e. album_id, but through the album name - i.e. t.string :name

class CreateSongs < ActiveRecord::Migration
  def change
    create_table :songs do |t|
      t.string  :name
      t.string  :album_name
      t.integer :album_id
      t.references :album
      t.timestamps
    end
  end
end

class CreateAlbums < ActiveRecord::Migration
  def change
    create_table :albums do |t|
      t.string :title
      t.string :image_url
      t.timestamps
    end
  end
end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Kononenko, 2015-06-05
@premas

Why would you need to override keys for this? This is very bad. In the association, add either a foreign_key or an association_foreign_key depending on the type of association.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question