Rails 4.0.3 Active resource - rails-activerecord

s=Food.find("pizza") returns information as <
<#Food:0xcee080c #attributes={"a=>"5"},"food_groups"=>[#Food::FoodGroup:0xcee0028 #attributes = {"gname="pizzatype"}]
category is an instance method available in FoodGroup. When i access the following in rails console as s.food_groups.category
NoMethodError: undefined method `category' for Food::FoodGroup:0xcee080c
Why did Rails add the dependent class at the front. How can I access the FoodGroup category method?

Here s.food_groups returns a array.
So you have to call like s.food_groups.first.category

Related

Undefined method `collection' for #<Mongo::Client> Did you mean? collections (NoMethodError). Ruby

I am working with automated test. This is the first time I'm working with mongoDB.
So, I am trying to create a generic method to find a document in a desired collection that will be passed as parameter. I've found some examples and all of them use the .collection method. It doesn't seem to work in my project.
Here's my DB client code:
require 'mongo'
require 'singleton'
class DBClient
include Singleton
def initialize
#db_connection = Mongo::Client.new($env['database']['feature']['url'])
end
def find(collection, value)
coll = #db_connection.collection(collection)
coll.find(owner: 'value')
end
end
And here's how I instance my method
DBClient.instance.find('collectionTest', 'Jhon')
When I run my test I get the following message:
undefined method `collection' for #<Mongo::Client: cluster=localhost:>
Did you mean? collections (NoMethodError)
The gem I'm using is mongo (2.6.1).
What I am doing wrong?
Based on documentation, there is indeed no method collection in Mongo::Client. What you are looking for is the [] method. The code will then look like this:
require 'mongo'
require 'singleton'
class DBClient
include Singleton
def initialize
#db_connection = Mongo::Client.new($env['database']['feature']['url'])
end
def find(collection, value)
coll = #db_connection[collection]
coll.find(owner: value)
end
end
EDIT: I've also changed the line with the find itself. In your original code, it would find documents where owner is 'value' string. I presume you want the documents where owner matches the value send to the function.

Undefined method `year' for 1:Fixnum

When using asset_sync with a Rails application I set custom headers with this line:
config.custom_headers = { '.*' => { cache_control: 'max-age=315576000', expires: 1.year.from_now.httpdate } }
Doing the same in a Sinatra app throws a 'undefined method `year' for 1:Fixnum'. What can I use instead or how do I specify 1 year from now? The year method is available as it's just Ruby code, I think it's the '1' that's causing the problem.
Yes #year is a helper method available in Rails.. Not in Sinatra.. In core Ruby Fixnum class there is no method called Fixnum#year.
Use as below :
Date.today.next_year
Date#next_year method exist in pure Ruby.

undefined method `model=' for Qt::ListView

I am trying to write an application with Ruby and Qt. I have the following code:
list = Qt::ListView.new(window)
mod1 = MyModel.new #MyModel inherits from Qt::AbstractListModel
list.model = mod1 #<< Fails on this line
But it fails, telling me:
undefined method `model=' for #<Qt::ListView:0x0000000067e300>
Yet I see other posts on SO that use model=, and I see the method listed in IRB when I issue list.public_methods.
OK, the problem is that I wasn't calling super() in the initialize method of my model.

field vs method ruby on rails

I have this class:
class User
include Mongoid::Document
field :revenues, :type => Integer, :default => nil
attr_accessible :revenues
#now method
def revenues
return 1
end
end
Why in console I get 1 instead nil?
1.9.3-p125 :002 > u.revenues
=> 1
Which has priority, the method or the field? How can I created a method with the same features that a field?
The field macro is defined in Mongoid::Document. It is neither a syntatic feature from Ruby nor from Rails.
What's happening with your code is the following:
The field function creates for you some methods, one of them is called revenues.
When you create another method called revenues, you are in effect overwriting the previously defined method, therefore making it useless.
Short answer: I don't understand a zip about Mongoid, but chances are that your field still exists even after you defined oce again a method named revenues. The only drawback is that you cannot access it by calling myUser.revenues anymore.
Try to make a test: access your field with the notation some_user[:revenues] and see what happen :)
Best regards

Get a class by name in Ruby?

Having a string with the module and name of a class, like:
"Admin::MetaDatasController"
how do I get the actual class?
The following code works if there's no module:
Kernel.const_get("MetaDatasController")
but it breaks with the module:
ruby-1.8.7-p174 > Kernel.const_get("Admin::MetaDatasController")
NameError: wrong constant name Admin::MetaDatasController
from (irb):34:in `const_get'
from (irb):34
ruby-1.8.7-p174 >
If you want something simple that handles just your special case you can write
Object.const_get("Admin").const_get("MetaDatasController")
But if you want something more general, split the string on :: and resolve the names one after the other:
def class_from_string(str)
str.split('::').inject(Object) do |mod, class_name|
mod.const_get(class_name)
end
end
the_class = class_from_string("Admin::MetaDatasController")
On the first iteration Object is asked for the constant Admin and returns the Admin module or class, then on the second iteration that module or class is asked for the constant MetaDatasController, and returns that class. Since there are no more components that class is returned from the method (if there had been more components it would have iterated until it found the last).
ActiveSupport provides a method called constantize, which will do this. If you are on Rails, which I assume you are based on the name of your constant, then you already have ActiveSupport loaded.
require 'active_support/core_ext/string'
class Admin
class MetaDatasController
end
end
"Admin::MetaDatasController".constantize # => Admin::MetaDatasController
To see how the method is implemented, check out https://github.com/rails/rails/blob/85c2141fe3d7edb636a0b5e1d203f05c70db39dc/activesupport/lib/active_support/inflector/methods.rb#L230-L253
In Ruby 2.x, you can just do this:
Object.const_get('Admin::MetaDatasController')
=> Admin::MetaDatasController
i could be way off-base, but wouldn't eval return the class?
eval("Admin::MetaDatasController")
so eval("Admin::MetaDatasController").new would be the same as Admin::MetaDatasController.new

Resources