How to properly use ActiveYaml with Rails 3.0.4? - ruby-on-rails-3

I ran across ActiveHash/ActiveYaml while learning Rails and wanted to use it to load lookup data. After following the installation directions, I got the ActiveHash::Base stuff working. I'm trying to load data from a YML file that looks like this:
AK:
name: Alaska
abbreviation: AK
AL:
name: Alabama
abbreviation: AL
I have a class in my models folder called usstates.rb that looks like this:
class USState < ActiveYaml::Base
set_root_path "#{RAILS_ROOT}/config/constants/"
set_filename "USStates"
fields :name, :abbreviation
end
I've tried to place my YML file in both the /config/constants/ and models folder. Each time I try to do something in Rails Console like USState.first, I get the following error:
NameError: uninitialized constant USState
How do I get this to load the YML file and show the items? This also fails if I comment out the sets in the class.

Related

How to persist column descriptions in BigQuery tables

I have created models in my dbt(data build tool) where I have specified column description. In my dbt_project.yml file as shown below
models:
sakila_dbt_project:
# Applies to all files under models/example/
+persist_docs:
relation: true
columns: true
events:
materialized: table
+schema: examples
I have added +persist_docs as described by dbt as the fix to make column description appear but still no description appears in bigquery table.
My models/events/events.yml looks like this
version: 2
models:
- name: events
description: This table contains clickstream events from the marketing website
columns:
- name: event_id
description: This is a unique identifier for the event
tests:
- unique
- not_null
- name: user-id
quote: true
description: The user who performed the event
tests:
- not_null
What I'm I missing?
p.s I'm using dbt version 0.21.0
Looks consistent with the required format as shown in the docs:
dbt_project.yml
models:
..[<resource-path>](resource-path):
....+persist_docs:
......relation: true
......columns: true
models/schema.yml
version: 2
models:
..- name: dim_customers
....description: One record per customer
....columns:
......- name: customer_id
........description: Primary key
Maybe spacing? I converted the spaces to periods in the examples above because the number of spaces is unforgivingly specific for yml files.
I've started using the vscode yml formatter because of how often I run into spacing issues on these keys in both the schema.yml and the dbt_project.yml
Otherwise, this isn't for a source or external-table right? Those are the only two artifacts that persist-docs is unsupported for.
Sources unsupported persist_docs -> sources tab
External Tables unsupported (Can't find in docs again but read today in docs or github issue)
Also Apache Spark unsupported (irrelevant here) Apache Spark Profile
Also, if you're going to be working with persist_docs a lot, check out this macro example persist_docs_op that Jeremy left for a run-operation to update your persisted docs in case that's all you changed!

Rails can't map automatically "y" ending plural models, is it possible? [duplicate]

I have a model named "clothing" which I want to be the singlular (one piece of clothing). By default, rails says the plural is clothings. Right or wrong, I think it will be more readable if the plural is "clothes".
How do I override the plural naming convention? Can I do it right in the model so I don't have to do it over and over? How will this change how routes are handled (I am using restful architecture)?
I'm no RoR expert, but did find a possible approach. From the referenced site you can add inflection rule inside the config/initializers/inflections.rb file:
# Add new inflection rules using the following format
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'clothing', 'clothes'
end
For rails 2.3.2 and maybe 2+, you need to do it a little different:
ActiveSupport::Inflector.inflections do |inflect|
inflect.plural /^(ox)$/i, '\1\2en'
inflect.singular /^(ox)en/i, '\1'
inflect.irregular 'octopus', 'octopi'
inflect.uncountable "equipment"
end
Add this in your environment.rb file if you are trying to stop database pluralization
ActiveRecord::Base.pluralize_table_names = false
With Ruby 2.2.2 windows or linux for me best solve was :
ActiveRecord::Base.pluralize_table_names = false
class Persona < ActiveRecord::Base
end
personas = Persona.all
personas.each do | personita |
print "#{personita.idpersona} #{personita.nombre}\n"
end
p Persona.count

ActiveRecord find when include?() matches

I have a Resource model with attribute subcategory_list, which is a comma-separated list of subcategories. Is it possible to do a find_by_x method (or equivalent) that pulls only those resources that belong to a certain subcategory?
So if given:
Resource.create(subcategory_list: "Fun, Games") # resource 1
Resource.create(subcategory_list: "Fun") # resource 2
Resource.create(subcategory_list: "Games") # resource 3
I would need a query to get both resources 1 and 2 when my input is "Fun". I can return ONLY "Fun" but not "Fun, Games" with the following
Resource.find_all_by_subcategory_list("Fun")
=> resource 2 (but not resource 1)
Is there a way to modify this query to include "Fun, Games" as well?
If subcategory_list is a comma-seperated string:
Resource.where('subcategory_list LIKE ?', "%Fun%")
If subcategory is an associated model:
Resource.joins(:subcategories).where('subcategories.name = ?', "Fun")
I agree with MrTheWalrus, but you might also want to check out acts_as_taggable_on by Michael Bleigh. It looks like what you are doing is tags associated with Resource and the acts_as_taggable_on gem will add a lot of power without having to write a lot of additional code, including what you are trying to do now as well.

Rails 3 & i18n: "Object must be a Date, DateTime or Time object" error

I am trying to translate my Rails 3 application, read the primer at http://guides.rubyonrails.org/i18n.html#adding-date-time-formats and subsequently downloaded the corresponding yml file from https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale (de.yml in my case).
In one of my views I had this code (some_model#index) ...
<td><%= time_ago_in_words(some_model.created_at) %></td>
... which I changed in ...
<td><%=l time_ago_in_words(some_model.created_at) %></td>.
Unfortunately this gives me this error:
Object must be a Date, DateTime or Time object. "etwa ein Monat" given.
Any idea why this fails? The created_at column has been created in the database via standard Rails scaffolding (database is mysql using mysql2 gem).
If I strip the time_ago_in_words helper from the code ...
<td><%=l some_model.created_at %></td>.
... the translation works - but the datetime now is of course too long for my <td>.
I also tried to duplicated the distance_in_words section of the de.yml and rename it to time_ago_in_words but this did not work either.
Am I missing something obvious?
OK, there are 2 different methods in play here :
the l method takes a Date, a Time or a DateTime object and returns a formatted version based on your I18n rules.
the time_ago_words takes the same arguments and uses I18n to spit out a formatted string.
In your example, you're trying to use both! Put simply, all you need is <%= time_ago_in_words(some_model.created_at) %>.

RoR - Geokit plugin: why Location.find is giving me back an empty array? (Location is a model that "acts_as_mappable")

I have installed both geokit gem and geokit-rails plugin. I configured the plugin as shown here: http://github.com/andre/geokit-rails . Then I generated a new model - Location, which looks like this:
class Location < ActiveRecord::Base
acts_as_mappable :default_units => :kms
end
and a controller:
class TestController < ApplicationController
include GeoKit::Geocoders
include GeoKit::Mappable
def test1
#a=Geokit::Geocoders::YahooGeocoder.geocode 'Kaohsiung City, Taiwan'
#b=Location.find(:all, :origin => '100 Spear st, San Francisco, CA', :within => 5)
end
end
I also set up a Locations table with both lng and lat columns in my database and put my google key in /config/initializers/geokit_config.rb
And right now, altough #a is giving me correct results from YahooGeocoder (I can use GoogleGeocoder as well), #b is an empty array. I know #a is generated using Geokit gem and #b using Geokit-rails plugin, so the problem is definitely related to the plugin. When I use #c=IpGeocoder.geocode('85.128.202.178') I get "success: false" ... What am I doing wrong here?
Have you double checked your Location collection to ensure there exists a location within 5 kilometers of that street address? What happens if you do a similar lookup with an explicit latitude and longitude of a Location?
Re: #success=false, I'm having the same trouble with that IP (using IpGeocoder and MultiGeocoder).
>> #c=IpGeocoder.geocode('85.128.202.178')
=> #<Geokit::GeoLoc:0x1038df5f0 #success=false, #city="(Unknown City)", #province=nil, #street_address=nil, #lng=nil, #full_address=nil, #state=nil, #country_code="PL", #all=[#<Geokit::GeoLoc:0x1038df5f0 ...>], #lat=nil, #precision="unknown", #provider="hostip", #zip=nil>
>> #c=IpGeocoder.geocode('74.125.93.104')
=> #<Geokit::GeoLoc:0x1038d3c28 #success=true, #city="Manila", #province=nil, #street_address=nil, #lng=120.95, #full_address=nil, #state=nil, #country_code="PH", #all=[#<Geokit::GeoLoc:0x1038d3c28 ...>], #lat=14.5833, #precision="unknown", #provider="hostip", #zip=nil>