我的Db正在改变结构,我在change
方法
这是我的模特:
Project(id: integer,
created_at: datetime,
updated_at: datetime,
creator_id: integer,
brand_id: integer,
order_id: integer,
real: boolean,
state: string,
finished_work_url: string)
Order(id: integer,
created_at: datetime,
updated_at: datetime,
brief_id: integer,
state: string,
delivered_at: datetime)
LegalOption(id: integer,
project_id: integer,
custom_document_file_name: string,
custom_document_content_type: string,
custom_document_file_size: integer,
custom_document_updated_at: datetime)
Proposal(id: integer,
created_at: datetime,
updated_at: datetime,
brief_id: integer,
creator_id: integer,
description: text,
price: float,
deposit_percentage: integer)
我正在使用project_id
更改列proposal_id
,这是另一个模型。所以我想为每个project_id
检索LegalOption
,然后选择正确的proposal_id
。
我试图在订单和项目之间使用我的关联,但由于rails约定,我遇到了错误The method .order() must contain arguments
。
现在,我同时创建了2个模型,我想使用方法created_at
来获取与我的项目相对应的顺序。但我有错误:
undefined method `created_at' for #<Project:...
我不明白为什么我不能使用这种方法。
这是我的迁移代码:
class ChangeDbStructureLegal < ActiveRecord::Migration
def change
old_db_structure_hash = {}
LegalOption.all.each do |legal_option|
old_db_structure_hash[:project_id] = legal_option.project_id
project = Project.where(id: old_db_structure_hash[:project_id])
Order.all.each do |o|
if o.created_at == project.created_at
proposal = Proposal.where(brief_id: o.brief.id, creator_id: project.creator.id)
old_db_structure_hash[:proposal_id] = proposal.id
end
end
rename_column :legal_options, :project_id, :proposal_id
legal_option.proposal_id = old_db_structure_hash[:proposal_id]
end
end
end
答案 0 :(得分:0)
您正在使用where
,它将始终返回关联,而不是单个对象,因此您需要添加.first
或使用find_by。
保持它在哪里,你试图在列表上调用.project_id,而不是一个不起作用的对象
class ChangeDbStructureLegal < ActiveRecord::Migration
def change
old_db_structure_hash = {}
LegalOption.all.each do |legal_option|
old_db_structure_hash[:project_id] = legal_option.project_id
project = Project.find_by(id: old_db_structure_hash[:project_id])
Order.all.each do |o|
if o.created_at == project.created_at
proposal = Proposal.find_by(brief_id: o.brief.id, creator_id: project.creator.id)
old_db_structure_hash[:proposal_id] = proposal.id
end
end
rename_column :legal_options, :project_id, :proposal_id
legal_option.proposal_id = old_db_structure_hash[:proposal_id]
end
end
end