سلام دارم یه دوره از آموزشهای ActiveRecord رو میگذرونم. کسی که داره آموزش میده از ریلز ۴ استفاده میکنه و من ریلز ۵.
من سه جدولِ customers, employees و جدولِ time_entries دارم که کارش رابط شدن بین دوتا جدوله درست کردم.
کدهای migration به این صورتن:
class CreateCustomers < ActiveRecord::Migration[5.0]
def change
create_table :customers do |t|
t.string :name
t.string :about
t.string :email
t.integer :balance
t.timestamps
end
end
end
class CreateTimeEntries < ActiveRecord::Migration[5.0]
def change
create_table :time_entries do |t|
t.float :time
t.belongs_to :customer
t.belongs_to :employee
t.timestamps
end
end
end
class CreateEmployees < ActiveRecord::Migration[5.0]
def change
create_table :employees do |t|
t.string :name
t.string :email
t.timestamps
end
end
end
و کدهای modelها به این صورت:
class Customer < ApplicationRecord
has_many :time_entries
end
class Employee < ApplicationRecord
has_many :time_entries
end
class TimeEntry < ApplicationRecord
belongs_to :employee
belongs_to :customer
end
اما مشکل جایی شروع میشه که میخوام این دستور رو توی کنسول ریلز اجرا کنم که با خطاهای زیر مواجه میشه:
2.3.1 :002 > TimeEntry.create(time: 1.2, customer_id: 1)
(0.3ms) BEGIN
Customer Load (0.8ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 1 LIMIT 1
(0.3ms) ROLLBACK
=> #<TimeEntry id: nil, time: 1.2, customer_id: 1, employee_id: nil, created_at: nil, updated_at: nil>end
از قبل هم یه Customer درست کردم پس مشکلی هم از او ن لحاظ نیست. نمیدونم کجای قضیه رو اشتباه رفتم واسه همین کلی اسم تاپیک رو زدم associations.
ممنون.