My site name and url are not recognized from my mailers - ruby-on-rails

I'm new at ruby on rails, and I've noticed that when I send an email, my site_name and site_url vars are not being recognized from the mailer files.
In my config.yml file I've got the following:
development: &local
site_name: my site name
site_url: http://localhost:8282
company_name: my company name
admin_email: admin#domain.com
support_name: askdjaskd Support
support_email: support#domain.com
production:
<<: *local
staging:
<<: *local
test:
<<: *local
And in the mailers I call them the as follows:
#body[:url] = "http://#{Setting.get(:site_url)}/"
Do I need to write a require statement at the beginning of the mailers? If not, what am I missing?
Thanks,
Brian

I've solved it! Instead of Settings.get(:site_url), I'm using configatron.site_url and it works!
Thank you!
Brian

Related

ActiveRecord::ConnectionNotEstablished (No connection pool with 'primary' found.)

The postgresql table looks like:
id | name | size | md5sum
----+-----------------------------------------+------+----------------------------------
1 | MyPreferredGenomeName | 1000 | df768fde8e10e15511c41895363bcce6
I am trying to retrieve the data:
myData = myClass.find_by name: "MyPreferredGenomeName" # also tried with other columns
# or
myData = myClass.where name: "MyPreferredGenomeName"
and I get the error stated in the title from activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1032:in retrieve_connection'`.
I don't have the most basic understanding of what's going on... Help.
Also, what does 'primary' stand for?
Thank you!
I suspect your config/database.yml is not configured correctly.
In that file you should have something similar to:
...
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: database_name_development
...
You want to make sure that database_name_development is the name of the database in your local PostgreSQL instance. Is your database actually called primary as that is what the error seems to be saying.
You show the psql output with the table named MyPreferredGenomeName so whatever database that is in is needed in database.yml.
You can test what database Rails is looking for with:
rails console
ActiveRecord::Base.connection.current_database
=> "database_name_development"
(Do not post any secrets in any replies e.g. db passwords or full connection strings.)

Custom yaml configuration in rails not working

I'm having some trouble loading custom configuration data from a yml file. I've looked at a few resources and can't seem to get anywhere with it.
When I try to load some custom settings from the yml file I get an empty hash.
my application.rb contains:
config.myapp = config_for(:myapp)
my myapp.yml contains:
default: &default
emails:
support: test#myapp.com
marketing: marketing#myapp.com
address: 123 Test lane
production:
<<: *default
development:
<<: *default
test:
<<: *default
When I call:
Rails.configuration.myapp
I get:
{}
Any thoughts what the issue might be?
Thanks
It's got something to do with the lifecycle of the Application object and where your config.myapp = config_for(:myapp) call sits.
So I tried it locally and solved this problem by putting it inside a config.before_initialize callback block:
this is in my config/application.rb file
config.before_initialize do
config.myapp = config_for(:myapp)
end
and then in rails console Rails.application.config.myapp correctly dumps the config parsed from yml.

Rails is not sending custom metrics to NewRelic

I'm trying to send custom metrics to NewRelic insights, but unfortunately it is not working for my Rails app that is currently sending default data to New Relic.
Steps to reproduce
I just logged in the console of the working application and ran the following command:
NewRelic::Agent.record_metric('/Custom/MyCategory/MyMetric', 5)
Unfortunately it never appeared in the Insights Data Explorer.
The configuration in the application is the following:
common: &default_settings
license_key: <MY_KEY>
app_name: my_app
log_level: info
development:
<<: *default_settings
app_name: executive_alerts (development)
monitor_mode: false
test:
<<: *default_settings
app_name: executive_alerts (test)
monitor_mode: false
staging:
<<: *default_settings
app_name: executive_alerts (staging)
production:
<<: *default_settings
Thank you!
From the documentation for the New Relic agent:
http://www.rubydoc.info/github/newrelic/rpm/NewRelic/Agent#record_metric-instance_method
https://github.com/newrelic/rpm/blob/c252fad410a5d41a65a827d633338af609f8dff6/lib/new_relic/agent.rb#L143-L144
metric_name should follow a slash separated path convention. Application specific metrics should begin with Custom/.
Change your command from:
NewRelic::Agent.record_metric('/Custom/MyCategory/MyMetric', 5)
to:
NewRelic::Agent.record_metric('Custom/MyCategory/MyMetric', 5)

How to set up database.yml

I am trying to set up ruby on rails to develop locally on a 64 bit windows 7 machine using SQL server 2005. We have an existing database and it does not conform to the ruby way so I created a table to test conectivity.
Unable to find information on properly configuring the database.yml file I suspect my problem is there. I have both 32-bit and 64-bit DSNs defined. When I run the console I get "Table doesn't exist"
Here's my database.yml. I don't know
default: &default
host: 111.222.333.444
adapter: sqlserver
mode: odbc
dsn: DSN_Name
database: ERP
username: sa
password: ********
development:
<<: *default
host: 111.222.333.444
adapter: sqlserver
mode: odbc
dsn: DSN_Name
database: ERP
username: sa
password: ********
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
Does anyone know how the default works? I can't find any information on it. Specifically "<<: *default"
I updated my database.yml file. Created a table for testing; used the plural and ID for the primary key:
CREATE TABLE [dbo].[Cars](
[ID] [int] NULL,
[Name] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
Created a model
class Car < ActiveRecord::Base
end
But I get "Table doesn't exist" after connecting to Car and entering 'Car'. Anyone have any idea what the next step would be to debug this?
The &default in the line default: &default, means make the key/value pairs under this YAML namespace available as the variable *default.
The <<: *default lines are taking those key/value pairs, and making them part of the other groups, so you only need to change values that are different from the default: group.
If all your databases are on the same machine, then your database.yml should look a lot like this:
default: &default
host: 111.222.333.444
adapter: sqlserver
mode: odbc
dsn: DSN_Name
username: sa
password: ********
development:
<<: *default
database: ERP_development
test:
<<: *default
database: ERP_test
production:
<<: *default
database: ERP_production
Since you are on sqlserver, you'll probably have to create all those databases yourself (instead of letting Rails do it for you).
from: https://github.com/rails-sqlserver/activerecord-sqlserver-adapter
Force Schema To Lowercase
Although it is not necessary, the Ruby convention is to use lowercase
method names. If your database schema is in upper or mixed case, we
can force all table and column names during the schema reflection
process to be lowercase. Add this to your config/initializers file for
the adapter.
ActiveRecord::ConnectionAdapters::SQLServerAdapter.lowercase_schema_reflection = true
Schemas & Users
Depending on your user and schema setup, it may be needed to use a
table name prefix of dbo.. So something like this in your initializer
file for ActiveRecord or the adapter.
ActiveRecord::Base.table_name_prefix = 'dbo.'

Rails with PostGIS

First of all, I'm using Rails3 with Ruby 1.9.2.
I have a problem using PostgreSQL with PostGIS. I've tried two gems:
https://github.com/nofxx/georuby
https://github.com/rgeo/activerecord-postgis-adapter
The problem is, that the first one from nofxx works well, but it doesn't provide rake tasks, so that the postgis.sql and spatial_ref_sys.sql get inserted into the database. I need this, because it's comfortable and essential using tests (create automatically the database, insert the postgis sql and perform the migrations when running "rake test"). The strange thing is, that in the README on github the author writes something about PostGIS helpers and rake tasks. But they are not implemented (or I'm blind).
The second gem listed above provides this functionality for rake perfectly! But I ran into an issue that I can't solve. I followed the instructions in the README, but everytime when I try to set a position on a spatial column, i get the following output:
ruby-1.9.2-p136 :010 > v = Venue.new :location => "POINT(102.0, 47.2)"
=> #<Venue id: nil, last_updated: nil, created_at: nil, updated_at: nil, location: nil>
ruby-1.9.2-p136 :011 > v.location
=> nil
As you can see, I set a location using WKT (well known text), but it's always nil. I also tried to save it to the database, but it's also nil.
I figured out, that when I try to use the Objects provided by RGeo
RGeo::Geos.factory.point(-122, 47)
it says, that the factory is nil. So, there must be a problem by creating the factory provided by RGeo. As mentioned in the README of the activerecord-postgis-adapter I used the following line in my Venue model to provide this support:
self.rgeo_factory_generator = RGeo::Geos.factory_generator
but it doesn't work.
So to find a solution, there are two ways: Finding a way to get the rake tasks into the georuby gem of nofxx, or solve the problem with the factory of the activerecord-postgis-adapter.
Hope someone can help me,
thx!
I'v spent about 4 hours on solving the same problem with "activerecord-postgis-adapter". The answer is simple : "NEVER USER COMAS IN WKT". In your situation it must be v = Venue.new :location => "POINT(102.0 47.2)" Then everything works just fine!
Finally I solved it on my own. I was playing around with rake tasks and I ran into some issues (like, you can't provide a password from script with psql...) that anoyed me.
But i found out, that you can provide a template option to the database.yml file. So, I just wrote:
development:
adapter: postgresql
pool: 5
encoding: unicode
database: myDatabase
username: root
password: *****
su_username: root
su_password: *****
port: 5432
template: template_postgis
test:
adapter: postgresql
pool: 5
encoding: unicode
database: myDatabase_test
username: root
password: *****
su_username: root
su_password: *****
port: 5432
template: template_postgis
production:
adapter: postgresql
pool: 5
encoding: unicode
database: myDatabase_production
username: root
password: *****
su_username: root
su_password: *****
port: 5432
template: template_postgis
Now every time I create my database (during tests and so on) all the PostGIS stuff gets added! Yeah!

Resources