我对rails很新,我正在尝试创建一个小应用程序。我有2个表客户端和项目。我有一个jqgrid表,我用来尝试添加项目。我的表单请求项目表的信息,插入后我希望它通过缩写(CLIENT_NM)找到client_id。现在,当插入行时,clients表中的外键值输入为NULL。如何使用client_nm来使用rails更新外表(如果密钥存在)。
The clients table Modes:
class Client < ActiveRecord::Base
database_connection = Rails.env
establish_connection database_connection.to_sym
self.table_name = 'clients'
has_many :projects, :inverse_of => :client
#attr_accessible :client_number, :client_mnemonic, :client_name, :client_location
end
实际表格:
SQL> desc clients
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER(38)
CLIENT_MNEMONIC VARCHAR2(255 CHAR)
CLIENT_NUMBER NUMBER(38)
CLIENT_NAME VARCHAR2(255 CHAR)
CLIENT_LOCATION VARCHAR2(255 CHAR)
CREATED_AT DATE
UPDATED_AT DATE
项目表:
class Project < ActiveRecord::Base
database_connection = Rails.env
establish_connection database_connection.to_sym
self.table_name = 'projects'
belongs_to :client, :inverse_of => :project
#attr_accessible :client_number, :client_mnemonic, :client_name, :client_location
end
实际项目表
SQL> desc projects
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER(38)
CLIENT_ID NUMBER(38)
CLIENT_NM VARCHAR2(255 CHAR)
PROJECT_NAME VARCHAR2(255 CHAR)
PROJECT_STATUS VARCHAR2(255 CHAR)
PROJECT_START DATE
PROJECT_END DATE
PROJECT_TYPE VARCHAR2(255 CHAR)
PROJECT_PLAN VARCHAR2(255 CHAR)
PROJECT_DESC VARCHAR2(4000 CHAR)
CREATED_AT DATE
UPDATED_AT DATE
控制器(snippit):
if params[:id] == "_empty"
Project.create(:client_id => params[:client_id],
:client_nm => params[:client_nm].upcase,
:project_name => params[:project_name],
:project_status => params[:project_status],
:project_start => params[:project_start].to_date,
:project_end => params[:project_end].to_date,
:project_type => params[:project_type],
:project_plan => params[:project_plan],
:project_desc => params[:project_desc])
也可能有助于显示jqgrid帖子表单数据:
colModel:[
{name:'id',index:'id', hidden:true},
{name:'client_id',index:'client_id', hidden:true},
{name:'client_nm',index:'client_nm', firstsortorder:"asc", width:25,editable:true,editoptions:{size:8}},
{name:'project_name',index:'project_name', width:60, align:"left",editable:true,editoptions:{size:16}},
{name:'project_status',index:'project_status', width:60, align:"left",editable:true,editoptions:{size:16}},
{name:'project_start',index:'project_start', width:60, align:"left",editable:true,editoptions:{size:16,
dataInit: function(element) {$(element).datepicker({dateFormat: 'yy.mm.dd'})}
}},
{name:'project_end',index:'project_end', width:60, align:"left",editable:true,editoptions:{size:16,
dataInit: function(element) {$(element).datepicker({dateFormat: 'yy.mm.dd'})}
}},
{name:'project_type',index:'project_type', width:60, align:"left",editable:true,editoptions:{size:16}},
{name:'project_plan',index:'project_plan', width:60, align:"left",editable:true,editoptions:{size:16}},
{name:'project_desc',index:'project_desc', width:60, align:"left",editable:true,editoptions:{size:16}},
{name:'created_at',index:'created_at', hidden:true},
{name:'updated_at',index:'updated_at', hidden:true}
],
我认为我需要(在sudo代码中)
Project.create(:client_id => find(client_by_client_nm(params[:client_nm].upcase),
或类似的东西。我真的不确定魔法是如何发生在这里的。任何帮助将不胜感激。
答案 0 :(得分:0)
所以这可能是一个黑客攻击,但我知道我将永远只能获得一条记录
if params[:id] == "_empty"
Project.create(:client_id => Client.where(client_mnemonic: params[:client_nm].upcase).pluck(:id)[0],
...
希望这有助于某人。