The following code returns the table-names How do I get the individual column names within the table?
ActiveRecord::Base.connection.tables
#=> ["products","listings"]
How do I get: Products.< all columns >
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.
Related
I am using rails 4 with sunspot which uses solr and I am trying to order a model by the field value of an associated model.
I have a number and the number has_many favorites associated to it (multiple users can favorite the number).
Each favorite stores a user_id.
I want to order the numbers where the user_id of an associated favorite matches the current_user.id.
something like order('number.favorites.user_id', current_user.id)
I have tried to index an array of favorites.user_ids in the number searchable boolean(:favorite_user_ids) { favorites.pluck(:user_id) }
but now I'm trying to think of how to order by inclusion in that indexed array.
For example: "order where the current_user.id matches an id in the number.favorite_user_ids array.
leftJoin strategy:
I also found leftOuterJoin directly in solr https://solr.apache.org/guide/7_2/stream-decorator-reference.html#leftouterjoin
I think if I do a left outer join where I use on current_user.id = favorite.user_id then after I can order by user_id
I am trying to filter out all the objects(photos) that are not in a scheduled post table. I have the sql statement working:
Select * from photos p left join scheduled_posts sp on sp.photo_url = p.url where p.trashed = false and sp.photo_url is null
I am trying to get this to work in rails using active record but the two tables are not associated with each-other and I having trouble using .join because of this and am getting an undefined method 'join' error. Here is my current code:
#objects = "#{#objects.table_name}".joins("left join scheduled_posts sp on sp.photo_url = p.url ").where("photo_url #> ?", nil)
"#{#objects.table_name}" is a string, and #joins is not a method on String. Instead of calling it on the table name, you need to convert the table name to the name of the model class, and then call #constantize on it to get the class constant, which you can call #joins on.
classify(#objects.table_name).constantize.joins # ...
With Rails 5, how do you write a finder that orders by a column that you don't necessarily want to return? I have
Person.select("id, name").order('votes DESC').limit(15)
meaning I only want to return the "id" and "name" attributes of the model, but I would like the model itself ordered by the "votes" column. However, running the above gives the error
ActiveModel::MissingAttributeError: missing attribute: votes
How do I write a finder that orders by a column I don't always want to return?
pluck should do the trick but it returns an array of [id, name].
Person.order('votes DESC').limit(15).pluck(:id, :name)
find_by_sql returns an array of ActiveRecord objects
Person.find_by_sql('SELECT id, name FROM persons ORDER BY votes DESC LIMIT 15')
Person.select("id", "name").order("votes DESC").limit(15)
Should do the trick. Just need to separate the attributes to be selected.
I have two tables, Table A is the parent, Table B is the child. Both tables have the column "order_number".
I want to find the value in the "order_number" column in Table A, that matches the value of the "order_number" column in Table B, then I'll save the "id" of the Table A record in another column of the Table B record.
I'm having trouble coming up with an activerecord query to find the matching column value. How should I do that?
Activerecord query to find the matching column value:
I think this should do it:
TableA.where(order_number: TableB.pluck(:order_number))
Then you can loop over it to update the tableB column for TableA id like below(assuming its called table_a_id)
tablea_records = TableA.where(order_number: TableB.pluck(:order_number))
tablea_records.each do |a|
b.where(order_number:a.order_number).update_all(table_a_id: a.id)
end
I have two models: Products and Tags through a products_tags join in a HABTM relationship.
I am currently defining my controller index as:
#stats = Product.all(:include => :tags).uniq
which returns an array. How can I return an Active Rel object? I tried added scoped, however received a no method error.
I need to find and list a unique list of tags, and be able to view what product each specif tag belongs to.
Try #stats = Product.includes(:tags).uniq.
Note that uniq turns the Relation into an Array; if you want to do something like SELECT DISTINCT you'll want to use Product.includes(:tags).select('DISTINCT whatever').