@@ -20,7 +20,7 @@ class Tea < Airrecord::Table
2020 self .base_key = " app1"
2121 self .table_name = " Teas"
2222
23- has_many :brews , class : ' Brew' , column: " Brews"
23+ has_many " Brews " , class : ' Brew' , column: " Brews"
2424
2525 def self .chinese
2626 all(filter: ' {Country} = "China"' )
@@ -31,34 +31,34 @@ class Tea < Airrecord::Table
3131 end
3232
3333 def location
34- [self [:village ], self [:country ], self [:region ]].compact.join(" , " )
34+ [self [" Village " ], self [" Country " ], self [" Region " ]].compact.join(" , " )
3535 end
3636
3737 def green?
38- self [:type ] == " Green"
38+ self [" Type " ] == " Green"
3939 end
4040end
4141
4242class Brew < Airrecord ::Table
4343 self .base_key = " app1"
4444 self .table_name = " Brews"
4545
46- belongs_to :tea , class : ' Tea' , column: ' Tea'
46+ belongs_to " Tea " , class : ' Tea' , column: ' Tea'
4747
4848 def self .hot
4949 all(filter: " {Temperature} > 90" )
5050 end
5151
5252 def done_brewing?
53- self [:created_at ] + self [:duration ] > Time .now
53+ self [" Created At " ] + self [" Duration " ] > Time .now
5454 end
5555end
5656
5757teas = Tea .all
5858tea = teas.first
59- tea[:country ] # access atribute
59+ tea[" Country " ] # access atribute
6060tea.location # instance methods
61- tea[:brews ] # associated brews
61+ tea[" Brews " ] # associated brews
6262```
6363
6464A short-hand API for definitions and more ad-hoc querying is also available:
@@ -67,7 +67,7 @@ A short-hand API for definitions and more ad-hoc querying is also available:
6767Tea = Airrecord .table(" api_key" , " app_key" , " Teas" )
6868
6969Tea .all.each do |record |
70- puts " #{ record.id } : #{ record[:name ] } "
70+ puts " #{ record.id } : #{ record[" Name " ] } "
7171end
7272
7373Tea .find(" rec3838" )
@@ -107,7 +107,7 @@ class Tea < Airrecord::Table
107107 self .table_name = " Teas"
108108
109109 def location
110- [self [:village ], self [:country ], self [:region ]].compact.join(" , " )
110+ [self [" Village " ], self [" Country " ], self [" Region " ]].compact.join(" , " )
111111 end
112112end
113113```
@@ -158,17 +158,16 @@ The `sort` option can be used to sort results returned from the Airtable API.
158158
159159``` ruby
160160# Sort teas by the Name column in ascending order
161- Tea .all(sort: { Name: " asc" })
161+ Tea .all(sort: { " Name" => " asc" })
162162
163163# Sort teas by Type (green, black, oolong, ..) in descending order
164- Tea .all(sort: { Type: " desc" })
164+ Tea .all(sort: { " Type" => " desc" })
165165
166166# Sort teas by price in descending order
167- Tea .all(sort: { Price: " desc" })
167+ Tea .all(sort: { " Price" => " desc" })
168168```
169169
170- Note again that the key _ must_ be the full column name. Snake-cased variants do
171- not work here.
170+ Note again that the key _ must_ be the full column name.
172171
173172As mentioned above, by default Airrecord will return results from all pages.
174173This can be slow if you have 1000s of records. You may wish to use the ` view `
@@ -181,7 +180,7 @@ calls. Airrecord will _always_ fetch the maximum possible amount of records
181180Tea .all(paginate: false )
182181
183182# Give me only the most recent teas
184- Tea .all(sort: { " Created At" : " desc" }, paginate: false )
183+ Tea .all(sort: { " Created At" => " desc" }, paginate: false )
185184```
186185
187186### Creating
@@ -192,16 +191,15 @@ Creating a new record is done through `#create`.
192191tea = Tea .new (" Name" => " Feng Gang" , " Type" => " Green" , " Country" => " China" )
193192tea.create # creates the record
194193tea.id # id of the new record
195- tea[:name ] # "Feng Gang", accessed through snake-cased name
194+ tea[" Name " ] # "Feng Gang"
196195```
197196
198- Note that when instantiating the new record the column names (keys of the passed
199- named parameters) need to match the exact column names in Airtable, otherwise
200- Airrecord will throw an error that no column matches it.
197+ Note that column names need to match the exact column names in Airtable,
198+ otherwise Airrecord will throw an error that no column matches it.
201199
202- In the future I hope to provide more convient names for these ( snake-cased),
203- however, this is error-prone without a proper schema API from Airtable which has
204- still not been released.
200+ _ Earlier versions of airrecord provided methods for snake-cased column names
201+ and symbols, however this proved error-prone without a proper schema API from
202+ Airtable which has still not been released._
205203
206204### Updating
207205
@@ -210,11 +208,7 @@ Airtable with `#save`.
210208
211209``` ruby
212210tea = Tea .find(" someid" )
213- tea[:name ] = " Feng Gang Organic"
214-
215- # Since the Village column is not set, we do not have access to a snake-cased
216- # variant since the mapping is not determined. For all we know, the correct column
217- # name could be "VilLlaGe". Therefore, we must use the proper column name.
211+ tea[" Name" ] = " Feng Gang Organic"
218212tea[" Village" ] = " Feng Gang"
219213
220214tea.save # persist to Airtable
@@ -236,7 +230,7 @@ providing the URL. Unfortunately, it does not allow uploading directly.
236230
237231``` ruby
238232word = World .find(" cantankerous" )
239- word[" Pronounciation" ] = [{url: " https://s3.ca-central-1.amazonaws.com/word-pronunciations/cantankerous.mp3}]
233+ word[" Pronounciation" ] = [{url: " https://s3.ca-central-1.amazonaws.com/word-pronunciations/cantankerous.mp3" }]
240234word.save
241235```
242236
@@ -274,14 +268,14 @@ class Tea < Airrecord::Table
274268 self .base_key = " app1"
275269 self .table_name = " Teas"
276270
277- has_many :brews , class: 'Brew', column: " Brews "
271+ has_many " Brews " , class : ' Brew' , column: " Brews"
278272end
279273
280274class Brew < Airrecord ::Table
281275 self .base_key = " app1"
282276 self .table_name = " Brews"
283277
284- belongs_to :tea , class: 'Tea', column: 'Tea'
278+ belongs_to " Tea " , class : ' Tea' , column: ' Tea'
285279end
286280```
287281
@@ -296,14 +290,14 @@ To retrieve records from associations to a record:
296290
297291``` ruby
298292tea = Tea .find(' rec84' )
299- tea[:brews ] # brews associated with tea
293+ tea[" Brews " ] # brews associated with tea
300294```
301295
302296This in turn works the other way too:
303297
304298``` ruby
305299brew = Brew .find(' rec849' )
306- brew[:tea ] # the associated tea instance
300+ brew[" Tea " ] # the associated tea instance
307301```
308302
309303### Creating associated records
@@ -328,20 +322,12 @@ around.
328322Tea = Airrecord .table(" api_key" , " app_key" , " Teas" )
329323
330324Tea .all.each do |record |
331- puts " # {record.id}: #{record[:name ]}"
325+ puts " #{ record.id } : #{ record[" Name " ] } "
332326end
333327
334328Tea .find(" rec3838" )
335329```
336330
337- ### Snake-cased helper methods
338-
339- When retrieving an existing record from Airtable, snake-cased helper names are
340- available to index attributes. These are _ only_ available on retrieved records,
341- and _ only_ if the column was set. If it's ` nil ` , it will not exist. That means
342- if you want to set column that has a ` nil ` value for a column type, you'll have
343- to fully type it out.
344-
345331### Production Middlewares
346332
347333For production use-cases, it's worth considering adding retries and circuit
0 commit comments