views:

66

answers:

3

Hi,

I am trying to finish building a Rails "Home Inventory" app as an example to help me learn rails. Following gives a general overview of what I am trying to achieve:

The main purpose of this application is to show a page with detailed information.

So, http://localhost:3000/living-room-couch would display the information regarding the couch.

Each Item, can belong one( or has one?) of three categories:

  1. Book
  2. Furniture
  3. Electronics.

Book has the following properties:
- isbn,
- pages,
- address,
- category

Furniture has following properties:
- color,
- price,
- address,
- category

Electronics has following properties:
- name,
- voltage,
- address,
- category.

--

Now on my View side, I have made 3 templates in rails, that contain elements suited to display an item belonging to one of the 3 categories. Template for Book shows isbn, and template for Electronics shows the voltage.

How do I model this in ActiveRecord? I will put it in English, maybe someone can help translate into Rails:

An Item, belongs_to or has_one category. Category can be one of the three: Book, Furniture, or Electronic.

I don't know how to do this. I know that each category like Book will be a model of its own, due to having different characteristics.

Do I need to have Category has a model too, because it would only consist of Book, or Furniture or Electronics. If I was to take the route of making Category a model of its own, how would I make it relate to a model such as Book.


--or

Would I just go this route (or join model perhaps):

class BookModel < ActiveRecord::Base
  has_many :categories
End

And then, select to which category belongs, based on the Model name.

I hope I put the question right, I am just so confused regarding this.

Thank You for your time.

A: 

I don't think you need the category model. You can just create the book, furniture and electronic models and one controller for each one.

Edit

No, there's no advantage. I just said to create different models/controllers because what you're modeling, in my opinion, are different things. But if you think they'll have a lot in common, I'd suggest for you to use single table inheritance. This way you'd have a single table for every item and can have only one controller, but each item would have one model.

j.
Is there an advantage to using multiple controllers, versus just relating the models using some form of relationships and just using one controller.I say that because the controller will have one purpose: to determine which category the item is and then render the appropriate template.
BriteLite
A: 

Why not have an Item have the following relationships:

belongs_to :book
belongs_to :furniture
belongs_to :electronic

You can then only actually set on of these, and can do a test of

if item.book
#do things
end

for each item, if you care about whether it's a book, etc. You can even use validations to make sure it only ever belongs to one thing at a time.

Jenny
I actually got that idea right as I was typing this question into S.O.So what you are saying is that I should have a model for every category? So far, I would have Item Model. And I would add additional models for Categories other than Books, Furniture, and Electronics correct?
BriteLite
Sure, though I'm not exactly sure why you would need an Item model (are you just trying to show that Books, Furniture and Electronics are all Items? Like a form of super class?), but in general, that's how I handle a Model that might belong to one or more other models.
Jenny
Just from your description, I personally wouldn't have an item at all, and just worry about Book/Furniture/Electronics. If you ever need to display everything in the database, then display @books, @furniture, etc. in a row? For example, I might have a Cat model and a Mouse model...but I wouldn't gain much from ALSO having an Animal model that both belong to unless I planned on doing something with the inheritance. If I ever want to show all animals, then I just show all cats, and all mice, you know?
Jenny
Thank you for your response Jenny.Apeacox below responded with a need for Polymorphic relation which I think makes sense. I mean, from what I understand, since an ITEM, has one CATEGORY. And the model CATEGORY consists of BOOK, or FURNITURE models(with appropriate database fields), it would make sense to have ITEM be a polymorphic, so that it can apply to BOOK, or FURNITURE. I mean in plain English, what I am trying to do is, find out how to make CATEGORY model consist of BOOK, and FURNITURE model, so that I can attach an ITEM to a category, so that in the view, I can render the rite templat
BriteLite
A: 

the Item model could be declared as polymorphic (se here: http://guides.rails.info/association_basics.html#polymorphic-associations)

so you'll declare Item:

class Item < ActiveRecord::Base
  belongs_to :category, :polymorphic => true
end

then the 3 models that act as category (I'll show one, the others are same):

 class Employee < ActiveRecord::Base
   has_one :item, :as => :category
 end

 # and so on...

this way you can associate an Item to one of the 3 models (Book, Furniture and Electronics). each instance of these models will have an 'items' atribute, like this:

@book.items # returns all the items associated to a @book object

You can also use has_many association using a polymorphic model.

For the views, you can use Nested Form Objects (see here: http://guides.rails.info/form_helpers.html). basically, in each form, you should nest a form to create an Item object. to follow the Book example, you'll have something like this:

 <%= form_for :book, @book do |form| %>
   <%= form.text_field :isbn %>
   <!-- other fields -->
   <%= fields_for @book.item do |item_form| %>
      <%= item_form.text_field :name %>
   <% end %>
 <% end %>

hope this helped you ;)

apeacox
Apeacox, Thank you for your response.I watched a Ryan Bates video the other day, and I somewhat understood Polymorphic relations. His example of a comment being applicable to different models made sense. I had a feeling I might need to apply the polymorphic relation here as well.Please verify if this the best approach: BOOK,FURNITURE all belong to model CATEGORY, which belongs to ITEM, or ITEM has one category(BOOK,or FURNITURE). Do I need the model category at all? If i do I would assume that I would make the ITEM model a polymorphic one, that can belong to any one of 3 CATEGORIES. Right?
BriteLite
yes, if you make Item polymorphic, it can belong to one of the 3 categories. you'll refer to them as 'category' (see the example in my answer). so an @item can do @item.category_id and @item.category_type to know the exact object (class and id of the model), then you can refer to @item.category.isbn if the category is a Book or @item.category.price if it's a Furniture
apeacox