On 26 May 2011 11:38, Caroline M. <li...@ruby-forum.com> wrote: > Colin Law wrote in post #1001149: >> On 25 May 2011 22:05, Caroline M. <li...@ruby-forum.com> wrote: >>> Colin, >>> >>> user_products table has photo, title, message, price, id. >>> >>> products table has title, price, category, image_url, and id. >> >> Would it be possible to combine the two tables (so that the user >> products and products are in the same table)? Either using STI or >> maybe just have some way of knowing what sort each product is. Then >> from the point of view of your cart life would be easy. >> >> Colin > > thanks colin, i was trying to think about this already but not sure > really what way to do it. im new to all this. i need the two separate > controllers for the tables because i want users to be able to use the > new/user_products path creating their own products, while on the > products controller the new path is not visible to users.
You don't have to have separate models in order to have two controllers, both controllers can access the same model. Possibly the simplest way, to avoid the complications of STI, would just be to put all the columns you need for both types of product in one table and maybe use a special entry in the category table to say that is is a user model. Or alternatively a new column to indicate the type (don't call it 'type' though or rails will think you are using STI). Then just leave the unused columns empty. > i prob also > need to add user_id to the user_products table. im not sure how to > combine the tables. im tryna fins stuff on net on STI but im not really > understanding it too well. Again this will be much easier if you stick to one table. I presume you know about ActiveRecord relationships, if not have a look at the Rails Guide on relationships. You can say Product belongs to User and User has many Products. This will require a user_id column in the products table. Then you could use the fact that the user_id is nil to indicate that it is an ordinary product rather than a user product, rather than use a special category. I am sure you know that then if you have a user in @user then @user.products will give you all his products. I am assuming here that user products are different products to normal ones (rather than just being 'ordinary' products that relate in some way to the user). If they are actually just the same products then you would want a different setup altogether. Colin > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.