I have a database with entries which I can fetch using ActiveRecord. Currently, using something like post.to_yaml yields:
!ruby/object:Post
concise_attributes:
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: id
value_before_type_cast: 1
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: user
value_before_type_cast: efy5qC5YmJNml23JowOUrlmfN0D2
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: content
value_before_type_cast: bol4
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: location
value_before_type_cast: '123'
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: timestamp
value_before_type_cast: '12:00'
new_record: false
The exact collection i'm returning is as follow: record = Post.order(:timestamp).offset(15 * 0).first(15)
This returned result contains several fields which will be returned to a Flutter application. The data will populate a widget with several fields such as content, date and location, all of which is returned by the above query.
I could use a Dart library to parse the YAML, but is there a better way to condense the returned values so that only the necessary fields are shown?
As per the description shared it seems like you have data from the database and you now need to select only particular fields that needs to be shown.
As per current scenario you could use something like:
post.as_json(only: [:content, :name, :location])
Else you could modify the query you are using by using select statement for selecting specific attributes from database.
Post.select(:name, :content, :location)
Hope it helps!!
Related
I'm using Amplify from AWS to build a small ecommerce project using React as frontend.
I'd like to know how I should write the "Product" and "Order" types in the schema in order to be able to write productId's to a product array in the Order table when users complete a purchase.
My schema.graphql file:
type Product #model {
id: ID!
name: String!
price: Int!
category: String!
images: [String]!
}
type Order #model {
id: ID!
products: [Product] #connection
}
My question is about the last line, do I need to define that [Product] connection there or I can use [String] to store product id's in a simple string array?
Point 1: In dynamoDB, you only need to define the data type of your partition key and sort key, and these can be string, number etc. For all the other attributes, you don't need to define anything.
Point 2: The dynamoDB designers prefer using a single table per application, unless it's impossible to manage data without multiple tables. Keeping this in mind, your table can be something like this.
Please observe: Only Id aka partition key and Sk aka sort key column is fixed here, all other columns can be anything per item. This is the beauty of DynamoDB. Refer to this document for dynamoDB supported data types.
I don't get what this means in the Rails tutorial:
group(*args) public
Allows to specify a group attribute:
User.group(:name)
=> SELECT "users".* FROM "users" GROUP BY name
Returns an array with distinct records based on the group attribute:
User.group(:name) [User id: 3, name: "Foo", ...>, #User id: 2, name: "Oscar", ...>]
I don't see the grouping with the example they gave...
Group is most useful (I think) if you are trying to count stuff in your database or if you join multiple tables. Let me give a few examples.
1.
If you want to know how many users there are in your data base with each name then you can do:
User.group(:name).count
this will return a hash looking something like this:
{ ann: 4, bert: 15, cecilia: 3 ... }
I do not know why there are so many Berts in your database but anyway...
2.
If your users have related records (for instance cars) the you can use this to get the first car included in your activerecord model (the reason it will be the first is because of how group works and is further explained in the link below)
User.joins(:cars).select('users.*, cars.model as car_model, cars.name as car_name').group('users.id')
Now all records in this result will have a method called car_model and one called car_name.
You can count how many cars each user has with one single query.
User.joins(:cars).select('users.*, count(cars.id) as car_count').group('users.id')
Now all records will have a car_count.
For further reading: Mysql group tutorial
Hope this shed enough light over groups for you to try them out a little bit. I do not think you can fully understand them until you worked with them a little bit.
I'm working on a rails 2 project. I'm trying to fetch a record from tags table by using find_by_* . Its giving different result.
May I know why is this working like this?
In my model:
existing = user.tags.find_by_name(tag)
in Log:
SELECT * FROM `tags` WHERE (`tags`.`name` = 'Ror') AND (`tags`.user_id = 1) LIMIT 1
RuntimeError (#<Tag id: 980191043, user_id: 1, name: "rOr", created_at: "2014-09-09 12:18:55", updated_at: "2014-09-09 12:18:55">):
Are you using MySQL?
If so it is likely it is doing a case insensitive comparison. Whether MySQL is case sensitive is based around the field collation of the column: http://dev.mysql.com/doc/refman/5.0/en/charset-column.html
I'm using the mongoid gem in Ruby. Each time I upsert, save or insert the same unique document in a collection, the Ruby instance shows a different id. For example, I have a script like so:
class User
include Mongoid::Document
field :email, type: String
field :name, type: String
index({ email: 1}, { unique: true })
create_indexes
end
u=User.new(email: 'test#testers.edu', name: "Mr. Testy")
u.upsert
puts u.to_json
The first time I run it against an empty or non-existent collection, I get this output
{"_id":"52097dee5feea8384a000001","email":"test#testers.edu","name":"Mr. Testy"}
If I run it again, I get this:
{"_id":"52097e805feea8575a000001","email":"test#testers.edu","name":"Mr. Testy"}
But the document in MongoDB still shows the first id (52097dee5feea8384a000001), so I know we are operating on the same record. If I always follow the upsert with a find_by operation, I get the right id consistently, but it feels inefficient to have to run an upsert followed by a query.
Am I doing something wrong? I'm concerned that I will be getting the wrong id back in an operation where someone is, say, updating his profile repeatedly.
I need to get a SQL Query that I can execute to extract certain data. Here are the details:
DB Type: MySQL
DB Name: adminDB
Table Name: licenseinfo
Field Name: LicenseNumber
So the data in the LicenseNumber Column looks like this: ( this is just a sample there are 300,000 Records)
SSCY-334-0W2-5AA
SSCY-238-2W1-5F9
SSCY-378-0W5-5BD
SSCY-312-061-5PO
SSCF-323-0R2-5FW
SSCF-548-2U1-5OL
SSCF-332-0G5-5BY
SSCF-398-041-5PE
I need to extract all records that have SSCF in the LicenseNumber
I tried query builder in SQLYog but I was getting syntax errors.
Can Someone please write this query for me.
Assuming the letters are always at the start:
SELECT * FROM licenseinfo WHERE LicenseNumber LIKE 'SSCF%'
If you really do mean that the text 'SSCF' can be anywhere in the license number you can use LIKE '%SSCF%' instead, but if your sample data is representative, I think this will return the same results but be slower.
I think this should work:
select * from licenseinfo where LicenseNumber like 'SSCF%'