"puts" method in rails 3 - ruby-on-rails

I've been doing some development with rails 3 and I was wondering why calling the "puts" method doesn't output to standard out.
How do I get this back?

Instead of puts, use logger.info:
logger.info #collection.inspect

You can also use the standard ruby way by calling STDOUT << 'your output'. Method put is not a rails speciality, it comes with ruby. If you use rails, you can also rely on the logger object.

Related

Ruby on Rails where_values method

In Ruby on Rails 4 there was where_values method:
User.with_all_credentials(creds).where_values
[
[0] "lower(email) = 'test'"
]
In Ruby on Rails 5 when I try to do the same it returns me that this method not exists. How can I get the same result in Ruby on Rails 5?
As it might be easily seen in the Rails code, it’s now where_clause.
(Because where lives now in CLAUSE_METHODS.)
i think this may help out:-
The replacement is either to use where_values_hash method or to introspect where_clause
https://apidock.com/rails/ActiveRecord/Relation/where_values_hash

Method run on error with MiniTest

How can I have a method be called on error when using Minitest with Rails, to generate some extra information about the error that just happened?
Have you considered using something like pry or debugger to get more information? I also find liberally sprinkling puts statements throughout the code helpful to ensure that methods I think are being called, are actually called and to show variable values.
This was as simple as:
def teardown
if !passed?
call_method_for_failure
end
end

inspect array in controller rails and ruby

I want to inspect my array with all relation (belognTo and hasMany) in ruby and rail without loop in controller file. is there any way such like a php? I have try this
abort(#microposts.inspect)
but error is coming. Who can do this ?
just use
logger.debug "microposts---------->#{#microposts.inspect}"
The above line will print the micropost object in your console log
I suggest that you check Ruby on Rails Debugging Documentation.

How to get generators call other generators in rails 3

I am experimenting with gem development, right now specifically generators. So far I have successfully created two generators that do their job just perfectly. These two generators are in the same directory.
However, right now I have to call each of them separately.
What I'd like to do is just call one generator and have that generator call all the other ones. Just would type
rails g generator_name
and this would call x other generators.
Does anyone know how would I got about doing this?
Help is much appreciated, thanks!
In your generator, you can just call
generate "some:generator" # can be anything listed by 'rails g'
for example:
module MyGem
class InstallGenerator < Rails::Generators::Base
def run_other_generators
generate "jquery:install" # or whatever you want here
end
end
end
By the way, if you are working on Rails 3 gems, this question can also help out:
Rails 3 generators in gem
Another possibility is to use something like
invoke 'active_record:model', 'foo bar:string baz:float'
which is not as clean as generate, but has one advantage: When your generator gets called via rails destroy, this call -- like may other of Thors actions -- will try to revoke the action of the generator you invoke.
There's a catch however: Probably due to Thors dependency management, this only works once per generator you want to call, meaning that a second invoke of the same generator will do nothing. This can be circumvented by using a statement like
Rails::Generators.invoke 'active_record:model', '...', behavior: behavior
instead. In this case you have to explicitly pass through the behavior of your generator (which is a method returning values like :invoke, :revoke and possibly others, depending on which command -- rails generate, rails destroy, rails update, etc. -- called your generator) to achieve the same result as above. If you don't do this, the generator you call with Rails::Generators.invoke will also be executed when running your generator with rails destroy.
Alternatively you could stick to invoke and try to tamper with Thors invocation system. See also here for example.
Generators are based off of Thor, so you can use the apply method.
This is what the Rails Templater gem does. (Here's a walk through the Rails Templater gem.)
Take a look at the scaffold generator that comes with rails.
/Users/XYZ/sources/rails/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb
def manifest
record do |m|
#....rest of the source is removed for brevity....
m.dependency 'model', [name] + #args, :collision => :skip
end
end
Here the scaffold generator is using the model generator. So take a look at the dependency method. You can find the API docs for it over here.

Is the Rails console running as a Controller?

It seems like Rails' console (script/console or rails console) is like in a controller, but self.class gives Object (Rails 3.0.1 with Ruby 1.9.2), so is it controller or none of M, V, or C?
It's nothing - as it's not part of your production stack, but just a tool that sets up a useful debugging environment for you.
And it's certainly not a part of your application, so it doesn't fit into the whole MVC model.
https://github.com/rails/rails/blob/3-0-1-security/railties/lib/rails/commands/console.rb
You can see it basically just parses command-line options and then starts an IRB session. Helper methods like "reload!" are defined here.

Resources