How to get nice formatting in the Rails console - ruby-on-rails

I want to get something like this to look nice:
>> ProductColor.all
=> [#<ProductColor id: 1, name: "White", internal_name: "White", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">, #<ProductColor id: 2, name: "Ivory", internal_name: "Ivory", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">, #<ProductColor id: 3, name: "Blue", internal_name: "Light Blue", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">, #<ProductColor id: 4, name: "Green", internal_name: "Green", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">]
This doesn't work:
>> ProductColor.all.inspect
=> "[#<ProductColor id: 1, name: \"White\", internal_name: \"White\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">, #<ProductColor id: 2, name: \"Ivory\", internal_name: \"Ivory\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">, #<ProductColor id: 3, name: \"Blue\", internal_name: \"Light Blue\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">, #<ProductColor id: 4, name: \"Green\", internal_name: \"Green\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">]"
And neither does this:
>> ProductColor.all.to_yaml
=> "--- \n- !ruby/object:ProductColor \n attributes: \n name: White\n created_at: 2009-06-10 04:02:44\n updated_at: 2009-06-10 04:02:44\n id: \"1\"\n internal_name: White\n attributes_cache: {}\n\n- !ruby/object:ProductColor \n attributes: \n name: Ivory\n created_at: 2009-06-10 04:02:44\n updated_at: 2009-06-10 04:02:44\n id: \"2\"\n internal_name: Ivory\n attributes_cache: {}\n\n- !ruby/object:ProductColor \n attributes: \n name: Blue\n created_at: 2009-06-10 04:02:44\n updated_at: 2009-06-10 04:02:44\n id: \"3\"\n internal_name: Light Blue\n attributes_cache: {}\n\n- !ruby/object:ProductColor \n attributes: \n name: Green\n created_at: 2009-06-10 04:02:44\n updated_at: 2009-06-10 04:02:44\n id: \"4\"\n internal_name: Green\n attributes_cache: {}\n\n"
Thoughts?

The y method is a handy way to get some pretty YAML output.
y ProductColor.all
Assuming you are in script/console
As jordanpg commented, this answer is outdated. For Rails 3.2+ you need to execute the following code before you can get the y method to work:
YAML::ENGINE.yamler = 'syck'
From ruby-docs
In older Ruby versions, ie. <= 1.9, Syck is still provided, however it
was completely removed with the release of Ruby 2.0.0.
For rails 4/ruby 2 you could use just
puts object.to_yaml

You should try hirb. It's a gem made to to pretty format objects in the ruby console. Your script/console session would look like this:
>> require 'hirb'
=> true
>> Hirb.enable
=> true
>> ProductColor.first
+----+-------+---------------+---------------------+---------------------+
| id | name | internal_name | created_at | updated_at |
+----+-------+---------------+---------------------+---------------------+
| 1 | White | White | 2009-06-10 04:02:44 | 2009-06-10 04:02:44 |
+----+-------+---------------+---------------------+---------------------+
1 row in set
=> true
You can learn more about hirb at its homepage.

Awesome print is nice too if you want an object indented. Something like:
$ rails console
rails> require "awesome_print"
rails> ap Account.all(:limit => 2)
[
[0] #<Account:0x1033220b8> {
:id => 1,
:user_id => 5,
:assigned_to => 7,
:name => "Hayes-DuBuque",
:access => "Public",
:website => "http://www.hayesdubuque.com",
:toll_free_phone => "1-800-932-6571",
:phone => "(111)549-5002",
:fax => "(349)415-2266",
:deleted_at => nil,
:created_at => Sat, 06 Mar 2010 09:46:10 UTC +00:00,
:updated_at => Sat, 06 Mar 2010 16:33:10 UTC +00:00,
:email => "info#hayesdubuque.com",
:background_info => nil
},
[1] #<Account:0x103321ff0> {
:id => 2,
:user_id => 4,
:assigned_to => 4,
:name => "Ziemann-Streich",
:access => "Public",
:website => "http://www.ziemannstreich.com",
:toll_free_phone => "1-800-871-0619",
:phone => "(042)056-1534",
:fax => "(106)017-8792",
:deleted_at => nil,
:created_at => Tue, 09 Feb 2010 13:32:10 UTC +00:00,
:updated_at => Tue, 09 Feb 2010 20:05:01 UTC +00:00,
:email => "info#ziemannstreich.com",
:background_info => nil
}
]
To integrate it by default with your irb/rails/pry console, add to your ~/.irbrc or ~/.pryrc file:
require "awesome_print"
AwesomePrint.irb! # just in .irbrc
AwesomePrint.pry! # just in .pryrc

>> puts ProductColor.all.to_yaml
Simply works fine!
Source: https://stackoverflow.com/a/4830096

May also be noted that you can use:
j ProductColor.all.inspect
to output in Json format rather than Yaml

I think this solution is the most accurate one. You should try this:
puts JSON.pretty_generate Entry.all.map(&:attributes)
This will give you a super nice output compare to YAML format:
[
{
"id": 44,
"team_id": null,
"member_id": 1000000,
"match_id": 1,
"created_at": "2019-04-09 15:53:14 +0900",
"updated_at": "2019-04-09 15:53:14 +0900"
},
{
"id": 45,
"team_id": null,
"member_id": 1000001,
"match_id": 1,
"created_at": "2019-04-09 15:53:36 +0900",
"updated_at": "2019-04-09 15:53:36 +0900"
},
{
"id": 46,
"team_id": null,
"member_id": 1000003,
"match_id": 1,
"created_at": "2019-04-09 15:56:40 +0900",
"updated_at": "2019-04-09 15:56:40 +0900"
},
{
"id": 47,
"team_id": null,
"member_id": 1000004,
"match_id": 1,
"created_at": "2019-04-09 15:56:48 +0900",
"updated_at": "2019-04-09 15:56:48 +0900"
}
]

Hi you can also try this in your script/console if
>> y ProductColor.all
not working for you.
Try this:
>> require 'yaml'
>> YAML::ENGINE.yamler = 'syck'
then
>> y ProductColor.all

I had some troubles making it work so I'll add my two cents to awesome_print
add this to your Gemfile, preferably in :development
gem 'awesome_print', require: 'ap'
then in
rails console
you can do
> ap Model.all
That's it. However you can also add
require "awesome_print"
AwesomePrint.irb!
to your ~/.irbrc, this way awesome_print will be required anytime you open the console and you can simply do
Model.all
without the need of typing ap

You may also try the following for a group of objects
Object.all.map(&:attributes).to_yaml
This will give you much nicer output, like
---
id: 1
type: College
name: University of Texas
---
id: 2
type: College
name: University of California
Calling to_yaml on attributes rather than the object itself saves you from viewing the full contents of the object in the output
Or puts Object.last.attributes.to_yaml for a single object
Shorthand is also available: y Object.last.attributes

Use irbtools gem.
It will automatically format the the console output plus you'll get tons of great features.

You might want to define ProductColor's inspect method to return something that you find nice. For example:
def inspect
"<#{id} - #{name} (#{internal_name})>"
end
After which the result of ProductColor.all will display as something like [<1 - White (White)>, ...]. Of course you should adjust the inspect method to your needs, so that it displays all the information you need in a style that you like.
Edit: also if the issue was the lack of line breaks in the output, you might try
require 'pp'
pp ProductColor.all
which should insert linebreaks where appropriate

To add to Alter Lago's suggestion for using AwesomePrint,
If you can't/shouldn't/don't want to add the awesome_print gem to your project's Gemfile, do this:
gem install awesome_print
Edit ~/.irb.rc and add this:
$LOAD_PATH << '/Users/your-user/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib'
require 'awesome_print'
(Making sure the path and version are correct, of course)

Related

Rails showing all data from database to index page how to stop it?

i am new in rails and i have generated a resource named a company name:string bio:text and ceo:string but my index view showing all the db records but i haven't done any inspet so why it showing all the data in index page?
it showing like this
[#<Company id: 4, name: "google", bio: "ad company", ceo: "sundar pichaif", created_at: "2021-03-15 12:43:18.821528000 +0000", updated_at: "2021-03-15 13:05:49.407986000 +0000">, #<Company id: 5, name: "basecamp", bio: "task management", ceo: "jason fried", created_at: "2021-03-15 13:27:58.781628000 +0000", updated_at: "2021-03-15 13:27:58.781628000 +0000">, #<Company id: 6, name: "Github", bio: "code", ceo: "Chris Wanstrath", created_at: "2021-03-15 13:30:15.510656000 +0000", updated_at: "2021-03-15 13:30:55.654182000 +0000">]
please help me out :(
open your index file where it shows the data from your database, please remove "=" for example: remove this <%= #companies %> and add like this <% #companies %>, the reason I am telling you to remove is that when you use "=" this sign with <%= ..... %>, it will show all the data in this erb brackets, so remove this and your issue will be resolve, Thanks

Incorrect association in fixtures

I'm following the guide http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures with Rails 4.1.1 and using named fixtures within one another in the https://github.com/codetriage/codetriage project. However when I try to reference one fixture from another it doesn't work:
# fixtures/issues.rb
issue_triage_sandbox_issue:
id: 4
comment_count:
url: https://api.github.com/repos/bemurphy/issue_triage_sandbox/issues/1
last_touched_at: 2012-11-10 22:20:24.000000000 Z
number: 1
created_at: 2012-11-10 23:23:45.281189000 Z
updated_at: 2012-11-10 23:23:45.281189000 Z
repo: issue_triage_sandbox
title: first test issue in sinatra
html_url: https://github.com/sinatra/sinatra/issues/1
state: open
and
# fixtures/users.rb
issue_triage_sandbox:
id: 1
user_name: bemurphy
name: issue_triage_sandbox
full_name: bemurphy/issue_triage_sandbox
language: ruby
created_at: 2012-11-10 21:50:48.351554000 Z
updated_at: 2012-11-10 21:50:48.351554000 Z
issues_count: 1
You can see that the issue should be loading the repo issue_triage_sandbox repo. But in my tests it's not:
issue = issues(:issue_triage_sandbox_issue)
puts issue.repo
# => nil
puts issue.inspect
#<Issue id: 4, comment_count: nil, url: "https://api.github.com/repos/bemurphy/issue_triage...", repo_name: nil, user_name: nil, last_touched_at: "2012-11-10 22:20:24", number: 1, created_at: "2012-11-10 23:23:45", updated_at: "2012-11-10 23:23:45", repo_id: 915227508, title: "first test issue in sinatra", html_url: "https://github.com/sinatra/sinatra/issues/1", state: "open", pr_attached: false>
Any ideas why the issue is being created with a reference to a non-existant repo?
I think it has to be with the id attribute in the repos fixture.
I'm using yml fixtures. I added the issue and the repo to the existing ones in the project.
issues.yml
issue_triage_sandbox:
user_name: bemurphy
name: issue_triage_sandbox
full_name: bemurphy/issue_triage_sandbox
language: ruby
created_at: 2012-11-10 21:50:48.351554000 Z
updated_at: 2012-11-10 21:50:48.351554000 Z
issues_count: 1
repos.yml
issue_triage_sandbox:
user_name: bemurphy
name: issue_triage_sandbox
full_name: bemurphy/issue_triage_sandbox
language: ruby
created_at: 2012-11-10 21:50:48.351554000 Z
updated_at: 2012-11-10 21:50:48.351554000 Z
issues_count: 1
From the console:
rake db:fixtures:load RAILS_ENV=test
rails c test
irb(main):001:0> i = Issue.last
=> #https://api.github.com/repos/bemurphy/issue_triage...", repo_name: nil, user_name: nil, last_touched_at: "2012-11-10 22:20:24", number: 1, created_at: "2012-11-10 23:23:45", updated_at: "2012-11-10 23:23:45", repo_id: 915227508, title: "first test issue in sinatra", html_url: "https://github.com/sinatra/sinatra/issues/1", state: "open", pr_attached: false>
irb(main):002:0> i.repo
Based on the code in your project here, it looks like Issues belong to Repos. I'm wondering if the fixtures are having trouble going "backwards" in that relationship.
You could try using ERB to get the ID of the proper fixture and apply it like so:
repo_id: <%= ActiveRecord::FixtureSet.identify(:issue_triage_sandbox) %>

Why does ActiveRecord association return two copies of every associated object

I have...
class Report < ActiveRecord::Base
has_and_belongs_to_many :elements
end
class Element < ActiveRecord::Base
has_and_belongs_to_many :reports
end
I'm seeing some very weird behaviour at the command line:
$Element.all
[#<Element id: 1, name: "fdafda", created_at: "2013-03-12 02:10:56", updated_at: "2013-03-12 02:10:56">,
#<Element id: 2, name: "Foo", created_at: "2013-03-14 10:46:56", updated_at: "2013-03-14 10:46:56">,
#<Element id: 3, name: "Bar", created_at: "2013-03-14 10:47:03", updated_at: "2013-03-14 10:47:03">]
$ Report.first.elements
[#<Element id: 2, name: "Foo", created_at: "2013-03-14 10:46:56", updated_at: "2013-03-14 10:46:56">,
#<Element id: 2, name: "Foo", created_at: "2013-03-14 10:46:56", updated_at: "2013-03-14 10:46:56">,
#<Element id: 3, name: "Bar", created_at: "2013-03-14 10:47:03", updated_at: "2013-03-14 10:47:03">,
#<Element id: 3, name: "Bar", created_at: "2013-03-14 10:47:03", updated_at: "2013-03-14 10:47:03">]
(rdb:2) Report.first.elements.uniq
[#<Element id: 2, name: "Foo", created_at: "2013-03-14 10:46:56", updated_at: "2013-03-14 10:46:56">,
#<Element id: 3, name: "Bar", created_at: "2013-03-14 10:47:03", updated_at: "2013-03-14 10:47:03">]
How is the duplication of elements in Report.first.elements even possible? And how can I stop it?
rails -v
Rails 3.2.11
$ ruby -v
ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-darwin12.2.1]
Found the answer. Had a report selector in the form at reports/id/elements/new. If the user selected the current report in the report selector, two links between the element and the report would be created in the join table.

Rails ActiveRecord :counter_cache not updated if assocation not set up before create. Intentional?

I've implemented a belongs_to relation with :counter_cache => true and I notice that the counter cache does not get updated if the relation was not set up before the initial save.
For instance, say a company has_many employees. If I do
company.employees << Employee.new(:name => "Joe")
The counter gets updated correctly but if I do
company.employees << Employee.create(:name => "Joe")
The counter remains unchanged.
For more details, here are the models:
class Employee < ActiveRecord::Base
belongs_to :company, :counter_cache => true
end
class Company < ActiveRecord::Base
has_many :employees
end
And here's a Rails Console session that demonstrates this:
Loading development environment (Rails 3.0.5)
ruby-1.9.2-p180 :001 > company_a = Company.create(:name => "ACME")
=> #<Company id: 1, name: "ACME", created_at: "2011-07-22 01:31:39", updated_at: "2011-07-22 01:31:39", employees_count: 0>
ruby-1.9.2-p180 :002 > company_a.employees << Employee.new(:name => "Bob")
=> [#<Employee id: 1, company_id: 1, name: "Bob", created_at: "2011-07-22 01:31:59", updated_at: "2011-07-22 01:31:59">]
ruby-1.9.2-p180 :003 > company_a.reload
=> #<Company id: 1, name: "ACME", created_at: "2011-07-22 01:31:39", updated_at: "2011-07-22 01:31:39", employees_count: 1>
ruby-1.9.2-p180 :004 > company_a.employees << Employee.create(:name => "Joe")
=> [#<Employee id: 1, company_id: 1, name: "Bob", created_at: "2011-07-22 01:31:59", updated_at: "2011-07-22 01:31:59">, #<Employee id: 2, company_id: 1, name: "Joe", created_at: "2011-07-22 01:32:28", updated_at: "2011-07-22 01:32:28">]
ruby-1.9.2-p180 :005 > company_a.reload
=> #<Company id: 1, name: "ACME", created_at: "2011-07-22 01:31:39", updated_at: "2011-07-22 01:31:39", employees_count: 1>
The documentation does say that the counter is incremented/decremented when the object is created/destroyed but I was thinking it should monitor updates as well to be useful. Otherwise, say, moving employees between companies would quickly result in counters that are totally off.
Is this the expected behavior? If so, what's the rationale? And if not, am I doing something wrong? I tried this in Rails 3.0.5 and Ruby 1.9.2
Thanks!

weird behavior with acts_as_taggable_on

Edit
The instructions on Github instruct you to use the gemcutter source for the gem. Currently, this installs version 2.0.5 which includes the bug I've detailed below.
#Vlad Zloteanu demonstrates that 1.0.5 does not include the bug. I have also tried with 1.0.5 and confirm that the bug does not exist in this version. People struggling with acts_as_taggable_on and owned tags on 2.x, rollback and wait for a fix..
For some reason, tags aren't showing up on a taggable object when an tagger is specified.
testing the post
class Post < ActiveRecord::Base
acts_as_taggable_on :tags
belongs_to :user
end
>> p = Post.first
=> #<Post id: 1, ...>
>> p.is_taggable?
=> true
>> p.tag_list = "foo, bar"
=> "foo, bar"
>> p.save
=> true
>> p.tags
=> [#<Tag id: 1, name: "foo">, #<Tag id: 2, name: "bar">]
testing the user
class User < ActiveRecord::Base
acts_as_tagger
has_many :posts
end
>> u = User.first
=> #<User id: 1, ...>
>> u.is_tagger?
=> true
>> u.tag(p, :with => "hello, world", :on => :tags)
=> true
>> u.owned_tags
=> [#<Tag id: 3, name: "hello">, #<Tag id: 4, name: "world">]
refresh the post
>> p = Post.first
=> #<Post id: 1 ...>
>> p.tags
=> [#<Tag id: 2, name: "bar">, #<Tag id: 1, name: "foo">]
Where's the hello and world tags? Miraculously, if I modify the database directly to set tagger_id and tagger_type to NULL, the two missing tags will show up. I suspect there's something wrong with my User model? What gives?
EDIT
Even stranger:
Post.tagged_with("hello")
#=> #<Post id: 1, ...>
It finds the post! So it can read the tag from the database! How come it's not showing up with Post#tags or Post#tag_list?
I recreated your project, using exactly the same classes.
This is my result:
>> Post.create
=> #<Post id: 1, created_at: "2010-05-18 09:16:36", updated_at: "2010-05-18 09:16:36">
>> p = Post.first
=> #<Post id: 1, created_at: "2010-05-18 09:16:36", updated_at: "2010-05-18 09:16:36">
>> p.is_taggable?
=> true
>> p.tag_list = "foo, bar"
=> "foo, bar"
>> p.save
=> true
>> p.tags
=> [#<Tag id: 1, name: "foo">, #<Tag id: 2, name: "bar">]
>> User.create
=> #<User id: 1, created_at: "2010-05-18 09:17:02", updated_at: "2010-05-18 09:17:02">
>> u = User.first
=> #<User id: 1, created_at: "2010-05-18 09:17:02", updated_at: "2010-05-18 09:17:02">
>> u.is_tagger?
=> true
>> u.tag(p, :with => "hello, world", :on => :tags)
=> true
>> u.owned_tags
=> [#<Tag id: 3, name: "hello">, #<Tag id: 4, name: "world">]
>> p = Post.first
=> #<Post id: 1, created_at: "2010-05-18 09:16:36", updated_at: "2010-05-18 09:16:36">
>> p.tags
=> [#<Tag id: 1, name: "foo">, #<Tag id: 2, name: "bar">, #<Tag id: 3, name: "hello">, #<Tag id: 4, name: "world">]
Therefore, I can not replicate your bug. I've tried it with both mysql and sqlite.
This is from my env file:
config.gem "mbleigh-acts-as-taggable-on", :source => "http://gems.github.com", :lib => "acts-as-taggable-on"
This is my gem version:
gem list | grep taggable
mbleigh-acts-as-taggable-on (1.0.5)
Can you post your gem version? Can you try to upgrade your gem? What DB are you using?
If it doesn't work, can you also post the output from tail -f log/development.log ?
EDIT: I'm using Rails 2.3.5
mbleigh released a commit that resolve this problem.
To obtain ALL the tags you can use owner_tags_on :
p = Post.first
p.owner_tags_on(nil, :tags )
This is the commit: http://github.com/mbleigh/acts-as-taggable-on/commit/3d707c25d45b5cc680cf3623d15ff59856457ea9
bye,
Alessandro DS

Resources