I am RoR noob so this might be a simple problem for someone. I have created two models - User and Fe开发者_开发技巧edback and I have two tables associated with them. users and feedbacks.
Now I want to create a relationship table with user_id as one column and feeback_id as the other column.
Do I create a model or just a migration? I am confused.
Here are my user and feedback migrations.
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "phone"
t.string "password_hashed"
t.string "password_salt"
t.boolean "isdeleted", :default => false
t.timestamps
end
end
def self.down
drop_table :users
end
end
class CreateFeedbacks < ActiveRecord::Migration
def self.up
create_table :feedbacks do |t|
t.text "feedback"
t.integer "rating"
t.boolean "isdeleted", :default => false
t.timestamps
end
end
def self.down
drop_table :feedbacks
end
end
Now do I create a model??? >rails generate model FeedbackUserJoinTable ? Or just a migration like this ??
class CreateFeedbackUserJoinTable < ActiveRecord::Migration
def change
create_table :feedbacks_users, :id => false do |t|
t.integer :feedback_id
t.integer :user_id
end
end
end
I like this answer to another SO question on has_and_belongs_to_many
. Basically use HABTM relationships if the relationships between users and feedbacks will remain simple.
If you foresee that these relationships won't remain simple, you should create a join model between the two and use has_many ..., :through => ...
. That way, when you want to add properties to the relationships between a particular pair of User and Feedback objects, you can define those properties on the join model.
You do not need to create a model. Just create the migration as you have written :)
精彩评论