I have an article model and a user model. The user can follow that article, but can't unfollow it. I'd like to keep track of new followers for an article, and to retrieve it for when I want to plot it. (Assume, for now, that plotting is not the problem.)
I'd like every article to have a list of the every date since that article was created. So it will have:
article.newfollowers[1/1/2012] = 35
article.newfollowers[2/1/2012] = 4
Every time a user follows an article I would do
This.newfollowers[Date()]++
Obviously saving such data means that each row in the article database has hundred of attributes (for each date).
How would I save the data within the article model? How do I declare/define such attribute?
If you don't need to query for the dates directly, take a look at ActiveRecord::Store. You'll need to be using Rails 3.2.0 at least, but would prevent you from having to add so many columns.
Another solution might be to use a Redis sorted set, using the date timestamp as the score, and the new followers as the value.
Related
I am using Rails 4. I have a Room model with hour_price day_price and week_price attributes.
On the index, users are able to enter different times and dates they would like to stay in a room. Based on these values, I have a helper method that then calculates the total price it would cost them using the price attributes mentioned above.
My question is what is the best way to sort through the rooms and order them least to greatest (in terms of price). Having a hard time figuring out the best way to do this, especially when considering the price value is calculated by a helper and isn't stored in the database.
You could load all of them and do an array sort as is suggested here, and here. Though that would not scale well, but if you've already filtered by the rooms that are available this might be sufficient.
You might be able to push it back to the database by building a custom sql order by.
Rooms.order("(#{days} * day_price) asc")
I can't find an efficient way to query Posts(PFObject) or Users(PFUser) classes and also have the isPostLiked(boolean) and isUserFollowed(boolean) included in the results array respectively.
Lets say, I have queried and received 25 Posts from the server. I want to fill in the like heart button with red if I have previously liked this Post. It would be very inefficient to query all the likes of these Posts and see if current user is contained in the results.
Is it possible to write a cloud code function to insert an 'isLiked' field to the query results and return it to the User for instance?
I am open to new strategies since I am stuck here. It is obvious that most of the social apps are having this need as a standard so there must be an effective solution. Thanks
Your best action is to rid yourself of the relational database thinking. It seems to me you have a separate Likes class that tracks which user likes which post.
In the NoSQL space you should focus on your queries when you plan your datamodel. Ask yourself this question:
How do I want to query my data?
In this use case, I'm thinking you might want to
Show how many likes a Post has
Maybe show which users did like the Post
Track whether the current user has liked a certain post
Maybe find all the Posts the current user has liked?
To solve this, I would do the following:
On the Post class, add a column likedby.
On the User class, add a column likedposts.
Both these columns are Array columns
Every time a user likes a post, you add a Pointer to the current user to the likedby array column for the Post AND a pointer to the post to the likedposts array column for the User.
This makes it very easy to
find how many likes a post has (number of elements in likedby)
list all the users that liked the post (using query.includeKey("likedby") on the Post)
check if the current user has already liked the post (if likedby array contains currentuser)
list all the posts a user has liked (using query.includeKey("likedposts") on the User).
Use the same logic for followings.
I have a model that has data that can change monthly. I'd like to track that data over time so that we can pull up to the last 24 months worth (creating line graphs and such).
What's the best way to store that kind of data in a Rails model?
Create a model that belongs_to the first table which stores the data and the time for that data.
When the value on the first model changes, you make a new row in the values table with the corresponding value and time.
I've recently started looking into Ruby on Rails, and I've set up a basic system to scan an parse and XML datasource, storing the elements in a MySQL database.
I'm intending to run the script as a rake task at set intervals, so want to track additions and updates, outputting the new, or changed, values to a text file.
I initially looked at using the before_save in order to write self.changes to a file, however the complexity arises as I'm retrieving data from two different pages and want to group the log output, e.g note each pricing row is a different record in the same table, ignore the variable names these are examples.
Item GUID
- Price US: #{old price} to #{new price}
- Price UK: #{old price} to #{new price}
The solution I'm currently looking to implement is appending a logged column to the table, if the data changes I can set this to changed, or new if the record has been added, and use this in a query to find records in which logged is not NULL, and group them by GUID. However as this will execute after the object has been saved I lose knowledge of the past values.
Is there a different approach I could take to achieve something like this?
Yes, there is a better way to do this. Take a look at these options you've got:
audited gem: https://github.com/collectiveidea/audited
paper_trail gem: https://github.com/airblade/paper_trail
espinita gem: https://github.com/continuum/espinita
I am using parse.com for my survey application, In that I am implementing like mechanism where I have set of two images which users will be able to see and they have to like one of them. which be part of my survey.
Now I am downloading 20 sets per query then asking user click More then i download next 20 sets n so on..
when I query all the 20 sets which user have already votes is getting downloaded again., so how do i stop that ? so I do not get those sets repeated again and again.
Have a look at the Anypic tutorial on parse.com how they use the Activity class to track likes, comments etc. Use this as a template for how to plan your data model as opposed to relational principals.
One possible solution is to store all voted photos in an array on i.e. a voting object, or even the user object, and query for photos that are NOT in this array.
You should store your voting operations in somewhere.
So let me analyze the two possibilities to store your operations:
In your local (in the device): If you decide store them in local, you will retrieve some objects (sets) from the Parse and then you will look for unvoted ones, and probably you will lose some of them, maybe all of them. So it is not feasible.
In the Parse: As i explained, you should store them in Parse.
You can do it using:
Relations. You can take a look at:
https://www.parse.com/docs/relations_guide#top
When retrieving new sets, you should get sets which the current user hasn't voted yet. You can do this with Relational Queries in Parse. You can take a look at the documentation about it:
https://www.parse.com/docs/ios_guide#queries-relational/iOS
Or creating a Join Table.
This would be a custom implementation of a new class where you join your user with a set. Maybe you can store additional info about voting operations, like the time of voting.