How to create a model without a primary key in rails

I want to create a "Relation" model that extends ActiveRecord :: Base, sets its table name to "questions_tags" and without a primary key. What should I do?

class Relation < ActiveRecord::Base set_table_name 'questions_tags' # set table name, right? # how to define 'no-pk'? end 

UPDATE


Hi guys. I know that using "create_table" can solve this problem, but this is exactly what I want to know: what is the magic of create_table(:id=>false) ? How to get the same effect without using create_table(:id=>false) ?
+6
ruby-on-rails activerecord
source share
3 answers

Create a migration that looks like this:

 class CreateQuestionsTags < ActiveRecord::Migration def self.up create_table :questions_tags, {:id => false, :force => true} do |t| ... t.timestamps end end def self.down drop_table :questions_tags end end 
+8
source share

If you want to create a pivot table as it looks from the table name, AR will handle this in the background.

However, if you want to create a table with a lot of feilds, then: 1) rename your table to "realtions", please 2) use the primary key "id"

There is no reason not to use the primary key in the table, and it is very likely that you may soon regret it.

+1
source share

Why don't you want a PC?

Active Record is expecting a PC, and I don’t see what harm it can do.

-7
source share

All Articles