Activerecord: List Columns in Table from Console

ActiveRecord: List columns in table from console

This will list the column_names from a table

Model.column_names
e.g. User.column_names

How to get all table columns as hash in rails from console?

In Rails, the active record class of a table name is the singular form, and capitalized.

So to get the columns for the products table, you would use:

Product.column_names

This provides an array of all the column names for the table represented by the ActiveRecord object, Product.

Rails: How to list database tables/objects using the Rails console?

You are probably seeking:

ActiveRecord::Base.connection.tables

and

ActiveRecord::Base.connection.columns('projects').map(&:name)

You should probably wrap them in shorter syntax inside your .irbrc.

How to get an array with column names of a table

Suppose you have a Post model:

Post.column_names
# or
Post.columns.map { |column| column.name }

It will return an array with column names of the table 'posts'.

How do I list out the column names and field types of an existing table in ruby on rails?

You can do: Post.columns and then iterate on the resulting Array.

See more methods at Class: ActiveRecord::Base

activerecord - how to get all column of joined tables

You can get columns in posts tables by doing further querying, something like -

Category.joins(:posts).collect{|category| category.posts.map{|post| post.attributes.merge(category.attributes) } }

This will give you a giant list of post and category attributes merged together for each Category.

But the point of doing the join on Category is to get a set of Categories that have certain follow certain join criteria. If we take the next example in the same guide,

Post.joins(:category, :comments)

This also gives you a list of Posts only, but the list contains only the posts that follow the join constraint, which is, they all have a category and a comment.

Getting details of other columns from active record joins

What you are seeing (only Review objects) is what you're supposed to see, Rails performs the join between Review, Devision, Company etc etc and brings you back the Review records resulting from that join. However the selected aliased columns should be available as methods on the Review records:

reviews = Review.joins(...)
reviews.first.company
reviews.first.division

Note that inspect method doesn't show non column attributes so your console might hide the aliased columns (company, division) but they are there.

Since it seems like you're not after only filtering but you need association data, you might be better off exchanging this with eager_loading since if you try to access the associations like that you will fire n+1 queries.

Review.includes(division: [{company: :region}, :departments, :employees]).select('reviews.id, companies.name as company, region.name, divisions.name as division).limit(10)

How do I check constraints on table columns using Rails?

Any ActiveRecord subclass has columns method, that returns all metadata about columns for underlying table:

User.columns.find { |c| c.name == 'email' }.null
# => false

can't see tables in specific database using rails console

Try below code:

a=ActiveRecord::Base.establish_connection(
:adapter=> "adapter_name",
:encoding=> "utf8",
:reconnect=> "false",
:database=> "db_name",
:pool=> "5",
:username=> "xxxx",
:password=> "xxxxxxx",
:host=>"xx.xx.xx.xx"
)

a.connection.tables


Related Topics



Leave a reply



Submit