5

I want to use a grep-like function in pry console in rails. For example, when I have a model like this:

pry(main)> show-model Post
Company
  id: integer
  title: string
  author: string
  content: text
  created_at: datetime
  updated_at: datetime

I want to filter the output like:

pry(main)> show-model Post --grep "string"
  title: string
  author: string

or

pry(main)> show-model Post --grep "content"
  content: text

It seems there is a --grep option for ls command in pry. I couldn't find a similar function with show-model command. Is there a way to do it?

ironsand
  • 14,329
  • 17
  • 83
  • 176
  • no there is not `grep` like utility inside rails pry gem sorry .... but can't you check model attributes data types manually .... ? – Haider Ali Feb 01 '15 at 10:11
  • 1
    You can `show-models --grep string` but that would list all models and only highlight the string matches. – wpp Feb 01 '15 at 11:40
  • @HaiderAli Yes, I can. But when the model become bigger, it's easier if there is grep-like function. – ironsand Feb 02 '15 at 03:36
  • @wpp Highlight are also helpful, I prefer to show only matched result, though. Thanks! – ironsand Feb 02 '15 at 03:37

3 Answers3

4

Old question, but it still pops up on top of a google search.

You can pass a flag to ls in pry that will basically do the same thing as piping to grep:

ls Post -G "content"

A full list of flags can be obtained by ls -h, as per this answer: https://stackoverflow.com/a/58879455/2709804

smilingfrog
  • 376
  • 3
  • 14
1

Currently, I'm using a column_names method and Array#grep for this purpose.

pry(main)> Post.column_names.grep(/string/)
[
   [0] "title"
   [1] "author"
]
ironsand
  • 14,329
  • 17
  • 83
  • 176
0

you cant do this directly but you can write output of command to a text file then use cat , grep on that see this answer https://stackoverflow.com/a/13380275

irb(main):015:0> f = File.new("statements.xml", 'w')
irb(main):016:0> f << Account.find(1).statements.to_xml
irb(main):017:0> f.close
Community
  • 1
  • 1
shalbafzadeh
  • 803
  • 1
  • 8
  • 20