質問編集履歴
1
コード記載
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,12 +2,148 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
|
5
|
+
informationモデルのnameというカラムにデータを保存する際、選択したチェックボックスのvalue値を配列として保存しました。
|
6
|
-
|
7
|
-
|
8
6
|
|
9
7
|
レコード保存結果をチェックボックスの値ごとにわけて表示したいのですが、modelの検索で配列をうまくとることができません。どのように検索すべきかご教授ください。
|
10
8
|
|
11
9
|
|
12
10
|
|
11
|
+
以下記載
|
12
|
+
|
13
|
+
#app/views/information/_form.html.erb
|
14
|
+
|
15
|
+
```
|
16
|
+
|
17
|
+
<div class="field">
|
18
|
+
|
19
|
+
<%= f.label :名前 %><br>
|
20
|
+
|
21
|
+
<%= f.check_box :name, {multiple: true, checked: @information.name.include?('cat')}, 'cat', nil %>cat
|
22
|
+
|
23
|
+
<%= f.check_box :name, {multiple: true, checked: @information.name.include?('nora')}, 'nora', nil %>nora-cat
|
24
|
+
|
25
|
+
<%= f.check_box :name, {multiple: true, checked: @information.name.include?('dog')}, 'dog', nil %>dog
|
26
|
+
|
27
|
+
</div>
|
28
|
+
|
29
|
+
```
|
30
|
+
|
31
|
+
#app/models/information.rb
|
32
|
+
|
33
|
+
```ここに言語を入力
|
34
|
+
|
35
|
+
class Information < ActiveRecord::Base
|
36
|
+
|
37
|
+
def name=(value)
|
38
|
+
|
39
|
+
str = ""
|
40
|
+
|
41
|
+
value.each do |val|
|
42
|
+
|
43
|
+
str += val.to_s + "/"
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
rslt = str.empty? ? nil : (str)
|
48
|
+
|
49
|
+
write_attribute(:name, rslt)
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
def name
|
54
|
+
|
55
|
+
read_attribute(:name).to_s.split('/')
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
```
|
62
|
+
|
63
|
+
レコード内容をinfojsonにjson形式で取得
|
64
|
+
|
65
|
+
#app/controllers/infojson_controller.rb
|
66
|
+
|
67
|
+
```ここに言語を入力
|
68
|
+
|
69
|
+
class InfojsonController < ApplicationController
|
70
|
+
|
71
|
+
def index
|
72
|
+
|
73
|
+
@information = Information.all #←変更はここ?
|
74
|
+
|
75
|
+
response.headers['Access-Control-Allow-Origin'] = '*'
|
76
|
+
|
77
|
+
response.headers['Access-Control-Allow-Methods'] = 'OPTIONS, GET, POST, PUT, DELETE'
|
78
|
+
|
79
|
+
response.headers['Access-Control-Request-Method'] = '*'
|
80
|
+
|
81
|
+
response.headers[''] = '' #クロスドメイン対策
|
82
|
+
|
83
|
+
render :json => @information.order("date DESC")#日付でならべたい
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
```
|
90
|
+
|
91
|
+
吐き出されるjsonはこんな感じ
|
92
|
+
|
93
|
+
```ここに言語を入
|
94
|
+
|
95
|
+
[
|
96
|
+
|
97
|
+
{
|
98
|
+
|
99
|
+
"id": 1,
|
100
|
+
|
101
|
+
"name": [
|
102
|
+
|
103
|
+
"cat",
|
104
|
+
|
105
|
+
"dog"
|
106
|
+
|
107
|
+
],
|
108
|
+
|
109
|
+
"date": "2015-10-23",
|
110
|
+
|
111
|
+
"img_name": "http://test.jpg",
|
112
|
+
|
113
|
+
"text": "テスト1",
|
114
|
+
|
115
|
+
"link_url": "https://www.google.co.jp/",
|
116
|
+
|
117
|
+
},
|
118
|
+
|
119
|
+
{
|
120
|
+
|
121
|
+
"id": 2,
|
122
|
+
|
123
|
+
"name": [
|
124
|
+
|
125
|
+
"nora",
|
126
|
+
|
127
|
+
"dog"
|
128
|
+
|
129
|
+
],
|
130
|
+
|
131
|
+
"date": "2015-10-22",
|
132
|
+
|
133
|
+
"img_name": "http://test.jpg",
|
134
|
+
|
135
|
+
"text": "テスト2",
|
136
|
+
|
137
|
+
"link_url": "",
|
138
|
+
|
139
|
+
},
|
140
|
+
|
141
|
+
```
|
142
|
+
|
143
|
+
たとえば上記で”name”に”nora”が含まれた場合のレコードだけを取りたいのです。
|
144
|
+
|
145
|
+
よろしくお願いします。
|
146
|
+
|
147
|
+
|
148
|
+
|
13
149
|
参考 [リンク内容](http://qiita.com/merrill/items/8ec158953cb4c2715c7b)
|