rails 3 meta_search usage - ruby-on-rails

I am in the process of upgrading my app from Rails2 to Rails3.
My Rails2 app uses searchlogic heavily.
After googling i've come to know that searchlogic is not compatible with Rails3 and need to use meta_search instead.
But i havent quite understood the usage of meta_search vis-a-vis searchlogic.
If i have a User model with :name and :address fields, i am not able to use the following methods with meta_search. What am i doing wrong?
ruby-1.9.2-p0 > User.name_null
NoMethodError: undefined method `name_null' for #<Class:0x000000038d5ce0>
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/base.rb:1008:in `method_missing'
from (irb):7
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
None of the methods like User.user_id_eq(1) or User.name_equals("Blah") are working.
I guess i havent understood the usage of meta_search yet!
Ref:
meta_search https://github.com/ernie/meta_search

The methods are attributes to be set in a FormBuilder. As such, you'll want to call user_name_equals = "Bob", not user_name_equals("Bob"). Also, they'll be on a search instance, not the model itself.
#search = User.search(:user_name_eq => "Bob")
If you're looking for something to use in day to day query construction, try MetaWhere instead. http://metautonomo.us/projects/metawhere

Keep an eye on rd_searchlogic, which looks to be compatible with Rails 3, though still a preview as of this writing.
EDIT
As described in this SO thread, install via:
gem 'rd_searchlogic', :require => 'searchlogic', :git => 'git://github.com/railsdog/searchlogic.git'

Related

Local variable in Ruby on Rails is undefined

I am currently following a recipe box tutorial on youtube.
When I open the rails console and type
#recipe = current_user.recipes.build
I got the following error:
NameError: undefined local variable or method `current_user' for main:Object
from (irb):3
from /Users/matthiascordes/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/console.rb:65:in `start'
from /Users/matthiascordes/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/console_helper.rb:9:in `start'
from /Users/matthiascordes/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
from /Users/matthiascordes/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /Users/matthiascordes/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:9:in `require'
from bin/rails:9:in `<main>'
Does anybody has an idea what is wrong and how I can solve this problem?
I am using the devise gem for my user model.
you can not access current_user inside your rails console,so try
user = User.find(your_user_id)
user.recipes.build
current_user is a variable provided by one of the before_filters of devise, therefore, outside a controller you don't have access to it, if you still want to be in that context, you can use gem byebug and drop a byebug inside your code, or a debugger and the execution will stop and you will be in the scope you need, to test all you are required, otherwise, plain in rails c, you will need to fetch the user the old way, with a query
User.find(user_id)
You cannot access current_user in rails console .
If you want the user variable, then search it like
user = User.find(some_id)
& then use it like user.recipes.build
you can also create the user from rails console like user = User.create(user parameters) & then use it building receipies

Wrong number of arguments when using Squeel

First, I do simple squeel testing in the Rails console.
User.where{name == "abc"}
There is no problem for new-created Ruby project
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."name" = 'abc' => []
However, when I apply it on my existing project, it gives me error message:
1.9.3p125 :001 > User.where{name == "abc"}
ArgumentError: wrong number of arguments (0 for 1) from /Users/xxx/.rvm/gems/ruby-1.9.3-p125/gems/parse_resource-1.7.3/lib/parse_resource/query.rb:11:in `where'
from /Users/xxx/.rvm/gems/ruby-1.9.3-p125/gems/parse_resource-1.7.3/lib/parse_resource/base.rb:243:in `where'
from (irb):1
from /Users/xxx/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /Users/xxx/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /Users/xxx/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
The most-related issue I found is Squeel issue #193.
It says that Squeel is not loaded. But, in my case, new-created can be implemented in the same machine.
Anyway, I try to change active_record.rb as it stated, but the problem is still here for my existing project.
I'm using Ruby-1.9.3-p125, Rails v3.2.6, and Squeel 1.0.14.
Well instead of using where clause you can use:
User.filter(:name => 'abc').first
This might work in your case

RubyOnRails: How do I use helper methods in Rails Console?

Probably something wrong with my setup:
irb(main):001:0> truncate("Once upon a time in a world far far away", :length => 17)
NoMethodError: undefined method `truncate' for main:Object
from (irb):1
from /usr/lib64/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /usr/lib64/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /usr/lib64/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Looks like I cannot use any text helpers (both in irb and rails console).
What should I check?
The Rails Console exposes the helper methods through the helper variable. Therefore, please, use this instead:
helper.truncate("Once upon a time in a world far far away", :length => 17)
for more, please read this article on 37signals.com
type following line into your rails console
include ActionView::Helpers
now your helpers are accessible during the entire rails console session and you can continue likeā€¦
truncate("Once upon a time in a world far far away", :length => 17)

Simple database association issue in rails

Newbie rails developer here.
I'm having an issue trying to setup some very simple database associations in a new rails project.
In my database I have two tables, one called "Games" and one called "Onlines". Here's what's in them right now
Game.first
#<Game id: 1, name: "Game 1", description: "This is a cool game", url: "http://domain.com">
Online.first
#<Online id: 1, game_id: 1, now: 222>
I'm trying to setup a simple association so I can grab the number of users online by doing something like...
Game.find(1).onlines.now
In my game.rb and online.rb models I have
belongs_to :online
and
belongs_to :games
accordingly.
When I try and run Game.find(1).onlines.now in the rails console I get the following error.
NoMethodError: undefined method `onlines' for #<Game:0x00000101654300>
from /Users/Jon/.rvm/gems/ruby-1.9.2-p180#rails3tutorial/gems/activemodel-3.0.6/lib/active_model/attribute_methods.rb:367:in `method_missing'
from /Users/Jon/.rvm/gems/ruby-1.9.2-p180#rails3tutorial/gems/activerecord-3.0.6/lib/active_record/attribute_methods.rb:46:in `method_missing'
from (irb):5
from /Users/Jon/.rvm/gems/ruby-1.9.2-p180#rails3tutorial/gems/railties-3.0.6/lib/rails/commands/console.rb:44:in `start'
from /Users/Jon/.rvm/gems/ruby-1.9.2-p180#rails3tutorial/gems/railties-3.0.6/lib/rails/commands/console.rb:8:in `start'
from /Users/Jon/.rvm/gems/ruby-1.9.2-p180#rails3tutorial/gems/railties-3.0.6/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Am I missing something ridiculously obvious here? I've tried everything I can think of.
You should have
has_many :onlines
in your Game model, not belongs_to.

Undefined method `html_safe?' error when using rails-ckeditor with formtastic

I'm trying to get the rails-ckeditor gem to work. I followed the instructions on the README.
But I get this error
undefined method `html_safe?' for #<String:0xb6b6d080>
This is my formtastic form code:
<%= f.input :content, :as => :ckeditor %>
Any ideas? Thanks!
UPDATE
I'm using Rails 2.3.8. And here's the stack trace.
/usr/lib/ruby/gems/1.8/gems/ckeditor-3.4.3/lib/ckeditor/safe_buffer.rb:6:in `<<'
/usr/lib/ruby/gems/1.8/gems/ckeditor-3.4.3/lib/ckeditor/view_helper.rb:52:in `ckeditor_textarea'
/usr/lib/ruby/gems/1.8/gems/ckeditor-3.4.3/lib/ckeditor/formtastic.rb:9:in `send'
/usr/lib/ruby/gems/1.8/gems/ckeditor-3.4.3/lib/ckeditor/formtastic.rb:9:in `ckeditor_input'
/usr/lib/ruby/gems/1.8/gems/formtastic-1.1.0/lib/formtastic.rb:1281:in `send'
/usr/lib/ruby/gems/1.8/gems/formtastic-1.1.0/lib/formtastic.rb:1281:in `inline_input_for'
/usr/lib/ruby/gems/1.8/gems/formtastic-1.1.0/lib/formtastic.rb:109:in `send'
/usr/lib/ruby/gems/1.8/gems/formtastic-1.1.0/lib/formtastic.rb:109:in `input'
/usr/lib/ruby/gems/1.8/gems/formtastic-1.1.0/lib/formtastic.rb:108:in `map'
/usr/lib/ruby/gems/1.8/gems/formtastic-1.1.0/lib/formtastic.rb:108:in `input'
/home/shreyas/repos/citymgmt/app/views/articles/_form.html.erb:4
/home/shreyas/repos/citymgmt/app/views/articles/_form.html.erb:2:in `_run_erb_app47views47articles47_form46html46erb_locals_form_object'
/home/shreyas/repos/citymgmt/app/views/articles/_form.html.erb:1:in `_run_erb_app47views47articles47_form46html46erb_locals_form_object'
/home/shreyas/repos/citymgmt/app/views/articles/new.html.erb:10
/home/shreyas/repos/citymgmt/app/views/articles/new.html.erb:3:in `_run_erb_app47views47articles47new46html46erb'
Are you running an earlier version of Rails than 3.0.0? You'll want to install the rails_xss plugin which provides this functionality. In Rails 3, this comes standard.
I would advise, if at all possible, to upgrade to Rails 3 as soon as you are able.
Do you have a stack trace? Based on the error, I'm assuming that the plugin load order is causing a string to not be instantiated with SafeBuffer support (which also leads me to believe you're using Rails 2).
Can you provide some context?

Resources