Within my rails app I have two models. A User model and a Book model. A user uploads and has many books.
I'm trying to add 10 predefined categories to the user model, so that a user assigns as many of the 10 categories to themselves as they wish. For example, if i'm a user that uploads a several books, and the categories I chose in my user settings are "fiction" and "science"
If another user chooses "fiction", "science", "history" as their categories, I want to be able to show them all books uploaded by users that are under these three categories including all the books I uploaded since I am under the categories of "fiction" and "science"
What is the best way to implement this? I was thinking of using the acts-as-taggable-on gem but maybe this is too heavy weight and necessary. Should I just add a string column to my user model where each category is separated by a comma, so that I can split them up into an array and do queries with Active Record?
Obviously you could create your own solution for the problem but ActsAsTaggableOn is a very good solution for it. It is not such a heavy weight as other gems.
Bonus: You can go back any time if it becomes too heavy for your application. Which I doubt.
Related
I am building an e-commerce app the hard way(no spree, shoppe, etc) so that I can learn rails. Currently, I have a restaurant model, meals model, and orders model. Users(using devise) can open meals, fill in an order's form with an attribute :quantity and send the order to their carts(keeping :restaurant_id, meal_id, :qty). However, meals in restaurants normally have sizes and supplementals(with cheese, with ketchup, etc.). Ideally, I would build something like spree_flexi_variants
but I just can't see how to do this... Should I nest "characteristics" and "supplementals"(becoming 2 levels deep as meals is currently nested in restaurant) in the meals, or somehow add an attribute to the meals model? Thank you guys for any suggestions!
I'm assuming that your meal object has the supplements as child items, in which case the order is also going to need child items, in this case they would be a join between supplements and order.
You don't HAVE to do it this way, it's just one possible approach. You could store the selected supplements as a serialised hash on the order row instead.
If this is a project aimed at learning, I would try both and see which feels nicest - it's all useful practice!
I'm building an ecommerce Rails application, and I want to create a relationship between Products and Users.
Users can "favorite" many products (and products can be favorited by users).
Users have a "history" about the products they saw.
In SQL databases, I know I can create, for example, a History table and put the product_id and user_id. And a Favorited table with also product_id and user_id.
But in MongoDB, how can I build this relationships?
I read this blog post: http://blog.markstarkman.com/blog/2011/09/15/mongodb-many-to-many-relationship-data-modeling/ and realized that I can create an array of products inside Users collection, and an array of users inside Products collection. But I don't know how to set what is the favorited product and the history product inside Users collection.
Thanks in advance.
EDIT
Thought: I'm thinking in create two models (ProductFavorited and ProductHistory) inheriting from Product model/collection and use it inside users collection.
The best data model in MongoDB is one that fits your application use.
So the questions to ask are "When are you going to see these favorited Products? How are they displayed?" and "When are you going to see this History? How is it displayed?"
Some possible answers and advice might be:
"A user will be able to click a menu item labeled 'Favorites' and see
a list of Products that they have favorited."
Then it makes sense to store the information you plan to show in a
favorites array on the User document. Or if you expect the User
document to be growing too large, a separate favorites collection
with just a user_id as _id and then just the array of favorites as
the only other element in the document.
Or:
"A user will go to a Product page and see a list of other users that have favorited that Product."
Then it makes sense to store it the other way with all of the users that favorited the product in the Product document. Or if you think the Product document will grow too large then in a separate collection with product_id as the _id and the favorited_users as an array which is the only other element in the document.
The thing to always keep in mind is how the data will be used in your application.
Information about Data Model in MongoDB.
(Rails newbie) I would like to offer users multiple checkboxes "Select All That Apply".
What is the usual way one handles that data? I was thinking I could just have a column that contained a series of selected strings.
Which foods do you like? Check all that apply
• Pizza
• Ice Cream
• Fried Chicken
• Tacos
If a user checked Pizza and Ice Cream and submitted, I was thinking I would have a column (SurveyQuestion1) that would look like "Pizza,Ice Cream".
I don't even know where to start here.
EDIT to be more specific:
the way I actually have it set up is I have a User model, a Product model and a Survey model. The form submits to all three models with nested attributes. The User enters info about themselves, the product they bought and a few surevy-ish/preference questions at the end. The User has_many Products and Surveys. The Products belongs_to User, as does Surveys. It's been working great for me until I've become hung up on this "select all that apply" type of question.
If you are using a SQL database, as it seems, you should take a look at many-to-many associations. Here's a RailsGuide about that: http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association.
You need to create a model for your foods. Then when a user selects his foods you store his selections on an association table that is going to have an entry for each user selected food.
Let's say that each Product has a category. I want to ask the Users to select several categories that the user is interested in, and find the Products that have the same category. This is similar to what Quora, Stumbleupon, and Pinterest all do.
What would be the best way to set this database structure in Rails? Should I create 3 tables: User, Product, and Category, and make the relations
User has many Categories & Product has many Categories?
The problem I see with this is doesn't it create, rather than reference, a new instance of Categories to each row of Users and Products?
*extra: What if I wanted subcategories? For example, if the user chose Technology, it could further ask to choose between web dev, mobile dev, hardware, etc.
You could do that kind of 'recommendation' pretty easily.
Something like this should work (N.B.: I did not test this code, but it is right in spirit):
def recommended_products
joins(:categories, :products).where("product_id not in (?)", self.products)
end
Explanation of each bit:
joins(:categories, :products): this does a SQL join of users, products, and categories. This gives you a 'table' where each user-product-category combination is in it's own row.
.where("product_id not in (?)", self.products): adds a SQL where clause to filter out all the rows that have products in the current user's list of products.
The associations are not a problem. They don't create any new instances by themselves, only if you write code that creates new instances yourself.
As for sub categories, I think you'll do better to make that it's own question, as it's easily a whole post in itself.
Hopefully we have good rails developer who can definitely give correct answer! For 2 days I didn't receive any valid answer for my question
I will explain in a very simple example
Customer is offering product. When he pushes create it gives form. Choose a category. Once he chooses another form will pop up.
Depending on a category, form should have totally different attributes.I can't have Product.new for every category. Reason is they have different attributes(Logicaly true). So do I have to create 100 models for 100 categories
Categories are : cars, apartments, coupons, books and many more
If you can give just one example I will be gratefull and call you expert
Thanks
It sounds like you're getting there. However, I wouldn't have a bunch of models like you're indicating in your question. I would say that you need a Product model and a Category model. The Category model will belong_to Product. The Product model would have many Categories. The Category model can use the acts_as_tree gem so that you can have categories and subcategories. Use javascript or jQuery (there was a recent Railscasts on this) to dynamically change and post a different field with a set of choices based on what was chosen.
EDIT:
I would have three Models; Product, Category, Specification
Product has many Categories
Product has many Specifications through Categories
Category belongs to Product
Category has many Specifications
Specification belongs to Category
This way I can create a product that has several categories. I can create several categories that have several specifications. Specifications are linked to the respective category. This will allow you to have three models and limited number of classes. Once your project is complete, new categories and specifications can be maintained by a web admin instead of a programmer.
This isn't the answer you want, but you're going to need a lot of models.
The attributes associated with an apartment (square meters, utilities, floor of building) are completely different from the attributes associated with a car (make, model, mileage, condition) which are completely different from a book (title, author, publisher, edition, etc). These items are so fundamentally different that there is no way to manage them in a single model.
That being said, there may be a core collection of attributes that might be associated with a product that is for sale (seller, price, terms). You have basically two paths forward:
You could decide to use Single Table Inheritance. In this case, you'd create an abstract class that defines the attributes that are common to all products that you are selling (seller, price, item). You'd then add a "type" column to your database that would be used to determine what type of product it is (mapped to your categories), and define all of the possible attributes in a single table.
You could choose a core set of attributes, and use these as a part of any other object that is considered a product. You'd have multiple tables that would have the full record for any given object.
Without knowing a lot of details about your application, it's hard to make a specific recommendation about which approach is right for you. Your best bet at this point is to spend a lot of time on google with "single table inheritance rails" and "multi table inheritance rails" and figure out which one is right for you (though my gut says multi table).