Call Method in Gem from Rails Model - ruby-on-rails

I've installed the name_of_person Gem into my Rails project.
This gem contains a method named full (source).
I have a Rails model named Film that contains a class named foo.
I want to call full from within foo, like so:
class Film < ApplicationRecord
def foo
name = NameOfPerson::PersonName.full("David Heinemeier Hansson")
end
end
When I try this I get this error: uninitialized constant Film::NameOfPerson.
I know I'm totally misunderstanding something basic about Ruby here. Please help.

Thanks #dbugger who helped me solve this one.
The problem appears to have been that the Rails gem wasn't installed correctly.
I uninstalled and re-installed the gem, and then I rebooted my Rails server. As soon as I did so both name = NameOfPerson::PersonName.full("David Heinemeier Hansson") and ::name = NameOfPerson::PersonName.full("David Heinemeier Hansson") work fine.
Thanks!

Related

Rails looking for wrong method

I have a column named _SOMETHING as part of an object created with rails g scaffold, say: rails g scaffold Person _SOMETHING:string
In the create method, when doing #person.save, an error pops up saying that it doesn't find method something(no underscore and lowercases).
Why is it looking for a method with that name?
I patched it by creating
def something
true
end
in my Person model. I am sure that's not the correct way to solve this.
Using Ruby 2.2.2, Rails 4.2.1
Thanks in advance
Turns out the model had a validation where I misspelled the column name, failing each time. Changed the name to _SOMETHING and it worked.
Thank you all for your attention to this silly issue

Conflict between Rails Admin and Impressionist gems

I'm using the latest version of the Impressionist and Rails Admin gems, and wondering if anyone could shed some light on an annoying conflict I'm experiencing. The problem is roughly documented here - https://github.com/sferik/rails_admin/issues/1315, yet the vaguely described solution is not working for me. When I have the line is_impressionable in my Listing model, I get an error when starting my Rails server with rails s:
...rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined local variable or method `is_impressionable' for Listing(no database connection):Class (NameError)
If I first start the server, and then add the 'is_impressionable' line, everything works fine, so the problem only occurs during initialization. I don't fully understand the initialization process, so am not sure how to go about getting this to work.
I have tried moving all my rails_admin model configuration options to their respective models, rather than in the initializer, which had no effect. I also have the following line in my initializer:
config.included_models = [Listing,ListingImage,AllOtherModelsHere...]
I have tried adding single quotes around these model names, which results in the following errors, as described in the github issue here
[RailsAdmin] Could not load model Listing, assuming model is non existing. (undefined local variable or method `is_impressionable' for Listing(no database connection):Class)
Any ideas what else I can try to make these gems work together? I don't want to have to remove the is_impressionable line every time I want to restart the server or generate a migration...
Not sure if the same issue that I had but yet I will post what worked for me just in case someone struggles with this too:
Im on a ruby 2.1.5 project with rails 4.2.0 and among other gems I'm using rails admin.
I run into several weird problems trying to set this up. For instance if I added the is_impressionable call within one of my models for some reason the execution of that file stopped there and I started getting weird errors like any method declared below the is_impressionable failed with undefined error.
So what I end up doing was:
class MyModel < ActiveRecord::Base
include Impressionist::IsImpressionable
is_impressionable
end
So this solved my issue and now i can access #my_model_instance.impression_count as expected.
I changed every occurrence of Klass to 'Klass'.constantize in initializer.

NameError in WelcomeController#index on RubyOnRails

I got this error
NameError in WelcomeController#index
uninitialized constant YouTubeIt
Rails.root: C:/Sites/rails_code/youtube
Btw i checked the gem author github, checked sintax of constant, seems everything's alright, so i can't figure it out.
There is a very similar question here:
Uninitialized constant SO
But in his case, there was a typo, i don't have any typo, i even checked the tutorial like 6 times, but there's no apparent error on my side, here's my controller code:
class WelcomeController < ApplicationController
def index
#cliente = ::YouTubeIt::Client.new(:dev_key => "AI39si4Ao5BFsYIkbzko7b9A_iktB2Pc8DAblJJ_JzJx6IL6Mju1dYYkMKY6TByz8MJPXfm4__tCAt9Is6Mvjg2JM55kuJVVqQ")
#videos = #cliente.videos_by(user: "AlbertoMaso2")
end
end
already installed the youtube_it and declared it's presence in the app by adding it to the Gemfile.
I'm stuck on this one and can't get it to work.
Anyone can shed some light upon this?
Thanks in advance!
When you have changed Gemfile, or added a new gem to it, please, make sure that you have updated Gemfile.lock also. To update lock file just run bundle install command.
Then in order to check usability the gem, you could not run whole rails server, but simply rails console with loaded development environment. Do it as follows:
$ rails c
or
$ rails console
Then inside the IRB, you can try your new gem features, in your case as follows:
irb(main):001:0> require 'youtube_it'
=> true
irb(main):002:0> YouTubeIt
=> YouTubeIt
What happens if you change
#cliente = ::YouTubeIt::Client.new...
to
#cliente = YouTubeIt::Client.new...

Convert an ActiveRecord column to PostGIS Point

My setup:
Ruby 2.0.0
Rails 3.2.12
most recent pg gem
most recent activerecord-postgis-adapter gem
most recent rgeo-geojson gem
Postgres 9.1.6
PostGIS 2
I've asked something similar a few days ago. (Need to convert a Boolean from Postgres (== String) to a Ruby Boolean). There I had to convert a value from a custom select to boolean. This was pretty straight forward, by just adding this to my model:
def value_name
ActiveRecord::ConnectionAdapters::Column.value_to_boolean(self[:value_name])
end
But now I receive a value of type Point (which is a type from PostGIS). Its string representation in database looks like "0101000000EE7C3F355EF24F4019390B7BDA011940", but it has to become a (I think) RGeo::Feature::Point or maybe RGeo::Geos::CAPIPointImpl ?!
Now I looked at ActiveRecord::ConnectionAdapters::Column (http://rubydoc.info/docs/rails/ActiveRecord/ConnectionAdapters/Column), I can only find value_to_boolean and value_to_decimal.
Then I recognized that there is also ActiveRecord::ConnectionAdapters::PostgreSQLColumn (http://rubydoc.info/docs/rails/ActiveRecord/ConnectionAdapters/PostgreSQLColumn), but it doesn't seem to have any useful methods.
Thank you!
Try something like that :
def value_name
point_regex = /POINT \(([0-9]*\.[0-9]*) ([0-9]*\.[0-9]*)\)/
match_data = self[:value_name].match(point_regex)
match_data[1], match_data[2]
end
It will return a couple of value [x, y] representing your point.
You have to do the inverse, i.e. define a value_name=(x,y).
I found a solution (searched for rgeo and parse):
def my_value
a = RGeo::WKRep::WKBParser.new
a.parse(self[:my_value])
end
I just need to know if it's the right way. Coming from the Java world, I read it like this:
For every(!) my_value: Create a new instance of WKBParser
If that's the case: How can I create just one instance of it and reuse it every time the method is called?
Or in more detail: How does the automatic parser handle this? Where does it get called?
I think it get's created here: https://github.com/dazuma/activerecord-postgis-adapter/blob/master/lib/active_record/connection_adapters/postgis_adapter/spatial_column.rb (Line 179 and 181)
But how can I reuse this in my model?
Background information: The parser automatically works for real table columns, but my column gets created within the query.
Found the right way:
def my_value
RGeo::Geos.factory.parse_wkb(self[:my_value])
end
:)
Yep, postgis adapter really works, and provides much more elegant solution.
In my Gemfile:
gem 'rails', '3.2.12'
gem 'pg'
gem 'rgeo-activerecord'
gem 'activerecord-postgis-adapter'
then in the model:
class MyPoint < ActiveRecord::Base
set_rgeo_factory_for_column(:geom, RGeo::Geos.factory(srid: 4326))
attr_accessible :geom
end
to find if Geos is supported in your RGeo installation:
>> RGeo::Geos::supported?
true
And that's what you get - model's attribute geom which is in this case RGeo::Geos::CAPIPointImpl (will vary depending on factory class). To get this factory working you need to have Geos with development files installed before installing RGeo. Doesn't have to be Geos factory and 4326, RGeo has factories implemented in pure Ruby, find your best match factory class and SRID in the docs: http://rubydoc.info/github/dazuma/rgeo/master/index

Rails overload gem models

I'm loading models through a gem, and I want to overload those models. I'm in Rails 3.
What is the best way to do this?
-= More Info =-
Ultimately, I want to run thinking_sphinx against a model in the gem so:
class User < ActiveRecord::Base
define_index do
indexes some_index
end
end
This takes over the gem's model. If I place it into the initializers, it works once then it won't continue to work on any rails server, WEbrick, unicorn, but it works fine in the console.
Thanks in advanced,
Justin
This was my answer.
How to monkey-patch code that gets auto-loaded in Rails?

Resources