Rails console - prevent large/text AR model attributes from being printed - ruby-on-rails

I have an ActiveRecord model with large text attributes which are I rarely need to see in console. The issue I'm currently facing is that they're so large that it makes it very difficult to see/notice other model attributes.
Is there a way to control its verbosity in Rails console and hide those(but not the other attributes) from being printed in console?
Usage is like:
> Foo.last
#<Foo:0x00007f47de244c30
id: 1,
name: "Short name",
description: "From Here to the Moon...........(truncated) and Back">
Rails version: 6.2
Ruby version: 3.1.2

Related

Collections not being read correctly after mongomapper is replaced by mongoid and mongo driver is updated

MongoDB V3.2
Upgraded the following the gems:
Ruby Mongo Driver from 1.11.1 to 2.10.4 + dependencies
Replaced MongoMapper 0.13.1 with Mongoid 5.4.1 + dependencies
After these changes, I noticed immediately that any collections that were placed in an additional module (FolderModuleName::ClassName) that could display data in MongoMapper would no longer display any data.
The only collections that would display data would be those without any modules for example a class that looked like this class DataClass.
I was able to figure out the issue by using the rails console and connecting to my database using the ruby mongo driver. (https://docs.mongodb.com/ruby-driver/master/quick-start/)
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'dbname')
db = client.database
db.collections # returns a list of collection objects
db.collection_names # returns a list of collection names
Using db.collection_names in the rails console I was able to see that any collection with a module was saved like this:
module_name.collection_name
After my upgrade the only collections names with modules I could read were:
module_name_collection_name
With this information, I added the following code to the ruby models affected:
store_in collection: 'module_name.collection_name'
This fixed my issue.
The reason collections without modules could be read without using the code above is because the collection names were simply stored as:
collection_name
Adding 'store in' in that case would just be redundant.

Rails model does not load properly

I updated my Rails application to a newer version of Rails, and now one of my models is unusable, as if it was loaded empty, as if Rails was not aware of its structure and how to populate it.
This model is Task and is based on Single Table Inheritance:
class Activity < ActiveRecord::Base
(...)
end
class Task < Activity
(...)
end
class Event < Activity
(...)
end
class Phase < Activity
(...)
end
Here is what is expected when the model is loaded properly and everything works well (same model because of STI):
Activity.inspect => Activity(id: integer, type: string, project_id: integer, author_id: integer, subject: text, description: text, (...), created_at: datetime, updated_at: datetime)
Task.inspect => Task(id: integer, type: string, project_id: integer, author_id: integer, subject: text, description: text, (...), created_at: datetime, updated_at: datetime)
Event.inspect => Event(same model as above ...)
(...)
However, since the update, Rails is unable to use the Task model at all, neither for reading a record from the database or creating a new one. Other related models Event and Phase works as expected.
When I debug it in production mode by inspecting the Task class and Task.first from the Task controller # show I get:
Task.inspect => Task()
Task.first.inspect => #<Task >
Task.first.nil? => false
This model appears as if it was void. Others models are loaded perfectly.
The application does not fail with an error when Rails tries to load the record, but it fails later when the controller tries to access any field, because the model is empty.
Some important observations :
It only fails in production mode, but works well in development mode, or it fails only when config.cache_classes = true but works well otherwise.
It works well in the Rails console.
task.rb model file gets loaded even in the failing context. Model files structure is flat, all models are in app/models/
It used to work well before updating from Ruby 1.9.3 and Rails 3.1 to Ruby 2.3.8 and Rails 3.2.22.
This is probably a duplicate of https://stackoverflow.com/a/58867450/1388302
Something else is defining Task - maybe to extend it. Don't do that. Use prepend. Otherwise the autoloader finds that unintentional definition and won't load the one you intend.
Or Task is now also defined by something you're including. It might be interesting to ask about Task.superclass.
This probably came up because Rails 6 changed the autoloader.
Something looks buggy with respect to Rails 3.2.22 schema loading and STI.
The best workaround I found so far is to force schema reloading for this model using :
Task.connection.schema_cache.clear!
Task.reset_column_information
But then Rails looses track of STI and will miss adding the "type" field to the newly created records. So to force this fields, in every controller required, I added :
#task = (...).becomes(Task)
This feels hacky, but I could not find any better way. Maybe this tip will help someone else. In my case it was not an option to upgrade Rails further or to refactor all the code to get rid of STI.

I can't populate data from seed.rb

I can't populate my data from seed.rb, when I use rake db:seed nothing happens.
Using Rails 5.
Data from Rails 4
Can I use this structure in Rails 5?
Product.delete_all
Product.create!(title: 'CoffeeScript',
description:
%{<p>
CoffeeScript is JavaScript done right. It provides all of JavaScript\'s
functionality wrapped in a cleaner, more succinct syntax. In the first
book on this exciting new language, CoffeeScript guru Trevor Burnham
shows you how to hold onto all the power and flexibility of JavaScript
while writing clearer, cleaner, and safer code.
</p>},
image_url: 'cs.jpg',
price: 36.00)
# . . .
Product.create!(title: 'Programming Ruby 1.9 & 2.0',
description:
%{<p>
Ruby is the fastest growing and most exciting dynamic language
out there. If you need to get working programs delivered fast,
you should add Ruby to your toolbox.
</p>},
image_url: 'ruby.jpg',
price: 49.95)
Product.create!(title: 'Rails Test Prescriptions',
description:
%{<p>
<em>Rails Test Prescriptions</em> is a comprehensive guide to testing
Rails applications, covering Test-Driven Development from both a
theoretical perspective (why to test) and from a practical perspective
(how to test effectively). It covers the core Rails testing tools and
procedures for Rails 2 and Rails 3, and introduces popular add-ons,
including Cucumber, Shoulda, Machinist, Mocha, and Rcov.
</p>},
image_url: 'rtp.jpg',
price: 34.95)
Seed Data Loading and Verifying
Create a test application
$ rails new seedtest
$ cd seedtest
Add product model
$ rails g model product title description image_url price:decimal
$ rails db:migrate
Prepare the db/seeds.rb file
puts 'start loading data'
copy and paste your loading code into db/seeds.rb
puts 'finish loading data'
Load Seed Data
$ rails db:seed
start Loading data
finish Loading data
Verify
$ rails db
sqlite> select * from products;
1|CoffeeScript|
CoffeeScript is JavaScript done right. It provides all of JavaScript's
functionality wrapped in a cleaner, more succinct syntax. In the first
book on this exciting new language, CoffeeScript guru Trevor Burnham
shows you how to hold onto all the power and flexibility of JavaScript
while writing clearer, cleaner, and safer code.
|cs.jpg|36|2017-02-11 10:44:38.648505|2017-02-11 10:44:38.648505
...
...
More data
.exit # when you've finished
Code on github - with commits using each title

Viewing what's in a model in Rails concole without Pry

I know that one can use Pry to see what a model in composed of but is there a way to see the various attributes of a model in the Rails console without Pry? What is the command?
I know I can go looking at the migration but I'd rather stay in the command line where possible.
Thanks
From script/console or irb you can just issue the name of the class and it will return the attributes.
For instance, with a Post model:
>> Post
=> Post(id: integer, title: string, body: text, created_at: datetime)

Why gives FactoryGirl such large ID's in my testcases?

I am using a Rails 2.3.x and Ruby 1.8.7 environment with FactoryGirl 1.3.3 (edit: version of FG has no influence as far as I can see)
In my console, when I do:
Factory(:user_activity)
I get:
#<UserActivity id: 25, user_id: 1, resource_id: nil, ... >
However when I do in my testcase:
#ua = Factory(:user_activity)
I get:
#<UserActivity id: 980190963, user_id: 298486374, resource_id: nil, ... >
Where are these large IDs coming from? How can I setup my testcases to use the expected IDs as seen in the console?
I suppose that the IDs are calculated in the same or an equivalent way as when using ActiveRecord fixtures (AR fixtures hashes the fixture label).
I don't know why using Factory in console will give you more normal IDs however.
Do you really need to know the ID in order to test? Or can you use your #ua variable to get the ID if you need it?

Resources