Ruby on rails 在rails 5中创建两个表之间的关系
我试图在rails中建立两个表之间的关系,以便 我可以在两个表之间共享数据。然而,我不能 将数据输入操作表。任何帮助都将不胜感激Ruby on rails 在rails 5中创建两个表之间的关系,ruby-on-rails,ruby,Ruby On Rails,Ruby,我试图在rails中建立两个表之间的关系,以便 我可以在两个表之间共享数据。然而,我不能 将数据输入操作表。任何帮助都将不胜感激 #below are models# class Location < ApplicationRecord has_many :operatings end class Operating < ApplicationRecord belongs_to :location end ##below are my tables## enable
#below are models#
class Location < ApplicationRecord
has_many :operatings
end
class Operating < ApplicationRecord
belongs_to :location
end
##below are my tables##
enable_extension "plpgsql"
create_table "locations", force: :cascade do |t|
t.string "country"
t.string "supra_region"
t.string "region"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "operatings", force: :cascade do |t|
t.string "operating_company_name"
t.string "address"
t.date "year_formed"
t.string "other_operational_countries"
t.string "about_company"
t.string "current_focus"
t.string "incumbent_irm_contractor"
t.string "irm_frame_agreements"
t.text "estimated_irm_budgets"
t.integer "location_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["location_id"], name: "index_operatings_on_location_id", using: :btree
end
add_foreign_key "operatings", "locations"
###below is my operating controller###
def create
@operating = Operating.new(op_company)
if @operating.save
flash[:success] = "A recorded has been successfully Saved"
redirect_to operatings_path
else
render 'new'
end
end
####routes####
resources :offshores, :index, :show, :new, :create, :destroy
resources :locations, :index, :show, :new, :create, :destroy
下面是一些模型#
类位置<应用程序记录
你做过很多手术吗
结束
类操作记录
属于:地点
结束
##下面是我的桌子##
启用扩展名“plpgsql”
创建表格“位置”,强制::级联do | t|
t、 字符串“国家”
t、 字符串“supra_region”
t、 字符串“区域”
t、 datetime“created_at”,null:false
t、 datetime“更新时间”,null:false
结束
创建表格“操作”,强制::级联do | t|
t、 字符串“运营公司名称”
t、 字符串“地址”
t、 “成立年份”日期
t、 字符串“其他国家/地区”
t、 字符串“关于公司”
t、 字符串“当前焦点”
t、 字符串“现任承包商”
t、 字符串“irm\u框架协议”
t、 文本“估算的irm预算”
t、 整数“位置\标识”
t、 datetime“created_at”,null:false
t、 datetime“更新时间”,null:false
t、 索引[“位置\u id”],名称:“位置\u id上的索引操作”,使用::btree
结束
添加外键“操作”、“位置”
###下面是我的操作控制器###
def创建
@运营=运营。新建(op_公司)
如果@operating.save
flash[:success]=“已成功保存录制的内容”
将\u重定向到操作\u路径
其他的
呈现“新”
结束
结束
####路线####
资源:离岸,:索引,:显示,:新建,:创建,:销毁
资源:位置,:索引,:显示,:新建,:创建,:销毁
试试看
您当前收到了哪些具体错误/意外结果?我根本无法将数据添加到手术台。我无法将数据输入到手术台您是否收到任何错误?op_company的值是多少?如果
@operating.save
失败,那么什么是@operating.errors.messages
?位置
模型的存在与所有这些有什么关系?(也许回答上述两个问题会揭示…@engineersmnky哦,是的,很好的观点…)。。。。如果问题中包含了错误消息,它仍然会非常明显!!OP,如果位置
不是强制关联,则需要使用属于:位置,可选:true
。如果它不是可选的,并且/或者错误消息不同,那么您需要提供更多详细信息-如上所述。
class Operating < ApplicationRecord
belongs_to :location, :optional => true
end
operating = Operating.create
operating.errors.messages