I have got data without "id" field from database. But when I parse the data to json string and show it on screen, I saw an "id" field on json string. How to remove this "id" field?
Here is my code:
-report_logic.rb
def fetch_data ()
@datalist=WorkEarlyOverTime
@datalist = @datalist.joins("INNER JOIN `works` ON `work_early_over_times`.`work_id` = `works`.`id`")
@datalist = @datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`')
@datalist = @datalist.where(work_id: $work_id)
return @datalist
end
- report_controller.rb
def work_late
report_logic = ReportLogic.new(params)
@work_late = report_logic.fetch_data()
render action: :work_late
end
- work_late.json.jbuilder
json.一覧 @work_late
When I show the string I expected the output is:
{"一覧":[
{"1次協力会社名":1,
"職種名":"0",
"作業場所":"0.00 ",
"作業内容":"2000-01-01T19:00:00.000+00:00",
"contents":"作業内容1"}
]}
but the actual output is:
{"一覧":[
{"id":null,
"1次協力会社名":1,
"職種名":"0",
"作業場所":"0.00 ",
"作業内容":"2000-01-01T19:00:00.000+00:00",
"contents":"作業内容1"}
]}
If id
is always null
then just ignore all nils:
json.ignore_nil!
json.一覧 @work_late
If that's not the case then you have to filter out the id
before serializing it. One way would be:
json.一覧 @work_late.serializable_hash.except("id")
As your desired output is a JSON
, you could try .as_json
by excepting the id field as follows:
@datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`').as_json(:except => :id)
Your detailed fetch_data function will be like this:
def fetch_data ()
@datalist=WorkEarlyOverTime
@datalist = @datalist.joins("INNER JOIN `works` ON `work_early_over_times`.`work_id` = `works`.`id`")
@datalist = @datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`').as_json(:except => :id)
@datalist = @datalist.where(work_id: $work_id)
return @datalist
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.