Randomly Showing Nomethoderror: Undefined Method 'Empty' for 0:Fixnum

Quickly create 3 ruby objects

As Roman already suggested, you should use an array.

However, to answer you question:

3.times do |i|
instance_variable_set("@part#{i + 1}", Part.create(part_number: "000#{i + 1}"))
end

Or even:

(1..3).each do |i|
instance_variable_set("@part#{i}", Part.create(part_number: "000#{i}"))
end

Ruby: using pack(n) with integers

You need to use an array, not integer("Hello".length is already one).

["Hello".length].pack("n")
# => "\x00\x05"

Ruby Fixnum comparison

If puts r['effortRemaining'].inspect comes out in quote marks, that means it is a string and you need to convert it to a number before you compare it.

This should work:

if r['effortRemaining'].to_f > 0

RSpec - Undefined method `key?'

get takes a action and hash of params (among other things). It will not implicitly take a model and turn it into { id: model.to_param }.

Instead you need to specify the parameters explicitly.

describe RestaurantsController do
let(:restaurant) { create(:restaurant) }

subject { response }

describe 'GET #show' do
before { get :show, id: restaurant }
it { should render_template('show') }
end
end

As @Зелёный has already mentioned you need to actually persist the record to the database to be able to use it in a controller spec.

To avoid the duplication issue and test ordering issues you should empty the database between each example. The database_cleaner gem is invaluable for that task.

Also if you need to create several records in the same spec you can use sequences in factory girl:

FactoryGirl.define do
factory :user do
email { |n| "test-#{n}@example.com" }
end
end

The gem ffaker is great for generating emails, usernames etc.

Added:

You can setup shortcuts for FactoryGirl by including its methods in your rails_helper:

RSpec.configure do |config|
# ...
config.include FactoryGirl::Syntax::Methods
end

This lets you use the FactoryGirl methods without typing the module name like so:

let(:restaurant) { create(:restaurant) }

undefined method `blank?' for 123:String (NoMethodError)

The blank? method is defined for every Ruby object that is descendant of the Object class in activesupport gem (https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/blank.rb).

This gem is part of Rails framework. However, if you still want to use this utility of activesupport in your non-Rails Ruby project, you can require it in your source files with the sentence:

require 'active_support/core_ext'

Make sure you have installed activesupport gem in your system.

How to only use tag_cloud for current_user?

Your issue is that acts-as-taggable-on does not actually use your Tag class. Or to be more specific - it works when you do Tags.top_20 since you are calling it on your tag class. But the User#tags relationship actually uses ActsAsTaggableOn::Tag which explains the NoMethodError.

ActsAsTaggableOn seems to have this functionality built in already though:

current_user.tags.most_used(20)

Addition:

I am combining the tags from several model instances into a single tag
cloud, which I am showing in the sidebar.

There is nothing in this requirement which says that you need to create a Tag or Tagging class. In fact doing so is likely to cause you and other developers a bunch of grief. The fact that the tag cloud on your home page works does not change the fact that you most likely are creating a bunch of future issues by replacing two relatively complex components (Tagging and Tag classes) with tiny stubs willy-nilly.

If you look at the tag_cloud implementation is pretty easy to see that it takes a ActiveRecord::Relation or a collection or just any old enumerable such as an array.

module ActsAsTaggableOn
module TagsHelper
# See the wiki for an example using tag_cloud.
def tag_cloud(tags, classes)
return [] if tags.empty?

max_count = tags.sort_by(&:taggings_count).last.taggings_count.to_f

tags.each do |tag|
index = ((tag.taggings_count / max_count) * (classes.size - 1))
yield tag, classes[index.nan? ? 0 : index.round]
end
end
end
end

Since collections are just fancy pants versions of arrays merging them together is as easy as:

@tags = foo.tags + bar.tags

However joining relations together is a bit more complex since Rails currently does not support the SQL OR clause. (Its coming in Rails 5). You would either have to load and merge the collections as above or create your own where clause with AREL if multiple SQL queries is a performance issue.



Related Topics



Leave a reply



Submit