Topic: What is the best modelstructure and routes?

Hello

I am sure that some Ruby-goeroes will be able to help me further (and in a much cleaner way).

I have a database where I have a lot of products.
Here are the most important column names:

+------+-----------------+-------------------+--------------+--------+-----------+
|ID      | Product class      | Product Group       | Item group     | Style    | Coating    |
+------+-----------------+-------------------+--------------+--------+-----------+
|001   |C                        |V0100                  |V0100600        |77         |ALU         |
|002   |C                        |V0100                  |V0100600        |77         |ALU         |
|003   |C                        |V0100                  |V0100600        |78         |ALU         |
+------+-----------------+-------------------+--------------+--------+-----------+

So:
A person will have to choose his coating, where after he will be able to
choose between the different product classes (which have that specific
coating).

Once a class has been chosen, he has to be able to see all product
groups with that class and that specific coating.

If he has chosen a product group, there has to be a list of all item
groups with a different style. So if there is a style 77 and a style 78,
this has to be seperated.

If he has chosen a certain item group + style, he will see a product
with all specifications.
As you see, there are 2 identical rows, but I left away some fields, for
specifications, that are different. Also the ID is different for each
product.

What is the best way to build my models and especially the resources?

Last edited by ReBa (2012-10-15 11:10:49)

Re: What is the best modelstructure and routes?

Is there nobody that can help me?
Or anybody that can tell me what extra information is needed?


Greetings

Re: What is the best modelstructure and routes?

Sure, I can help but I'm struggling to undrstand clearly exactly what you need help with, which is possibly why no one has answered your question yet.

It seems like you want to set up some has_many relationships with dynamic conditions or you can use a mixture of scopes and has_many relations
You might find this interesting
http://railscasts.com/episodes/202-acti … in-rails-3

If you can come back with a clearer simple, single question you would probably get a better response

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: What is the best modelstructure and routes?

The problem is that I can't set up any relationship.

It is one (1) table with all products in.

Product has:

+------+-----------------+-------------------+--------------+--------+-----------+----------+
|ID      | Product class      | Product Group       | Item group     | Style    | Coating    | Size        |
+------+-----------------+-------------------+--------------+--------+-----------+----------+
|001    |C                       |V0100                  |V0107200        |77         |GLV          | 24          |
|002    |C                       |V0100                  |V0107200        |77         |GLV          | 28          |
|003    |C                       |V0100                  |V0107800        |78         |ALU          | 24          |
|004    |C                       |V0100                  |V0107800        |78         |ALU          | 28          |
+------+-----------------+-------------------+--------------+--------+-----------+----------+

If the customer comes on the website, he has to choose between the different product classes.
--> There are different classes, from A to F

When he chooses a class, all product groups within that class (so a distinct sql query) has to show up.
--> A class has multiple product groups, but multiple products reside in a product group

When chosen an product group, he has to choose his item group (which stands for a product with different sizes)
--> So an item group is in fact 1 product, but with different sizes (24 - 28 ...)

The problem is that I have only one (1) table. I know you would say, make a table with the different classes, the different product groups etc. but I can't do that because the products table is changing every day with new products and deleted products (as record in the database). For that the other tables would have to change but that is impossible because the data I get is from a database that can't be changed.

So how do I make my model as correct so that I can have this selections?

I hope this is sufficient.

Thanks in advance!

Re: What is the best modelstructure and routes?

The problem is that I can't set up any relationship.

Why not?
http://railscasts.com/episodes/163-self … ssociation

So you need a distinct list of classes you need a distinct list of groups and you need a distinct list of groups within classes?

You have the option of using has_many or scopes or a combination of both.
I'm not seeing where you have the issue? Are you sure you are not over thinking this?

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: What is the best modelstructure and routes?

jamesw wrote:

So you need a distinct list of classes you need a distinct list of groups and you need a distinct list of groups within classes?

Kind of.
First they have the choise of 2 things (which I have to set up staticly I suppose) - I skipped this part in the previous posts

Choise 1: Electric (query: where unitnr = 20 OR status like "both")
Choise 2: Motoric (query: where unitnur <> 20)

If chose: All categories within this choise are being showed:

Electric:
A, B, D, F, E --> so my query would be: (select distinct from products where unitnr = 20 OR status like "both")

Motoric:
A, B, C --> so my query would be: (select distinct from products where unitnr <> 20)

If I understand, I should make a new table with this distinct categories? Where every product has its link to the category? A customer will never be able to make a new product. So as in the example, there won't be any creation of that relational table.

Or do you mean that I have to make new controllers according to the classes, productgroups and itemgroups?

jamesw wrote:

You have the option of using has_many or scopes or a combination of both.

With has_many it would look like:

Category:
has_many :productgroups
has_many :itemgroups
has_many :products
--> this seems to me like a very strange way of working (and I suppose that I am thinking wrong here)

With the scopes, I already have the distinct categories, productgroups and itemgroups seperate?
But the performance is than somewhat downsized because I have such a wide scope for the itemgroups (it could be that there are more than 2000 different itemgroups).

Re: What is the best modelstructure and routes?

First they have the choise of 2 things (which I have to set up staticly I suppose) - I skipped this part in the previous posts

Choise 1: Electric (query: where unitnr = 20 OR status like "both")
Choise 2: Motoric (query: where unitnur <> 20)

Do you have this set up and working?
That would be 2 scopes on the model

scope :electric, where("unitnur = ? OR status like?", 20, "BOTH")
scope :motoric, where("unitnur != ?", 20)

You are asking a lot for a single question and time is limited so I would like to deal with this one issue at a time
This allows you to do Product.electric or Product.motoric
Other named scopes can be chained to this
Does that help? If this is the right track then we just need to work out what other scopes you need and how to chain them effecientlty right?

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: What is the best modelstructure and routes?

jamesw wrote:
scope :electric, where("unitnur = ? OR status like?", 20, "BOTH")
scope :motoric, where("unitnur != ?", 20)

You are asking a lot for a single question and time is limited so I would like to deal with this one issue at a time
This allows you to do Product.electric or Product.motoric
Other named scopes can be chained to this
Does that help? If this is the right track then we just need to work out what other scopes you need and how to chain them effecientlty right?

(Maybe I also have to say that I am quite a newbie but willing to learn!)
Yes, I get that!

So in my main page I have:

<%= link_to "Electric", sessions_path(:arguments => {:selectionView => "prodclass", :electric => true}) %>
<%= link_to "Motoric", sessions_path(:arguments => {:selectionView => "prodclass", :electric => false}) %>

In my sessions_controller:

def index
   selectionParams = params[:arguments]
   if selectionParams
      if selectionParams[:selectionView] == "prodclass"
         if selectionParams[:electric]
            products = Product.electric
         else
            products = Product.motoric
         end
      end
   end

   @allfilteredproducts = products
end

In my Product.rb:

class Product < ActiveRecord::Base
   scope :electric, where("unitnr = ? OR status = ?", 20, "BOTH")
   scope :motoric, where("unitnr != ?",20)
end

So now I have my products that are OR electric OR motoric.

Lets continue with electric:

The scope :electric gives me 883 records. Within those records, I have these distinct categories:

A
C
D
F
M
S
V

Do I have to make a new scope for that than to?

Re: What is the best modelstructure and routes?

Good, but the session controller seems an odd plae to be posting to, nevermind, if it works then it's good for now and if problems crop up later this can easily be refactored

Do I have to make a new scope for that than to?

yup smile

scope :unique_categories, select(:categories).uniq

Should do the trick
You now have a number of options available to you as far as chaining is concenred
you can do stuff like

Product.unique_categories #get the first product record for each category
product.electric.unique_categories #Gets the first product record for each category that meets the electric conditions

etc...
product.electric.unique_categories would get you 7 product records with the categories have listed above (A, C, D, F, W, S, V). That is, you would get the first product record in each category as determined by the sort order on the table.
If you wanted all the records organised by category then use .group(:category) instead of .uniq I am suspecting you may have a requirement for both options

This might help make things a little clearer http://guides.rubyonrails.org/active_re … rying.html

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: What is the best modelstructure and routes?

Just a quick caveat - Obviously the code I have supplied above is not tested. There may be typos! but it should work

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: What is the best modelstructure and routes?

jamesw wrote:
Product.unique_categories #get the first product record for each category
product.electric.unique_categories #Gets the first product record for each category that meets the electric conditions

etc...
product.electric.unique_categories would get you 7 product records with the categories have listed above (A, C, D, F, W, S, V). That is, you would get the first product record in each category as determined by the sort order on the table.
If you wanted all the records organised by category then use .group(:category) instead of .uniq I am suspecting you may have a requirement for both options

if selectionParams[:electric]
  products = Product.electric
else
  products = Product.motoric
end
scope :unique_categories, select(:prodclass).uniq
scope :unique_categories, select(:prodclass).group(:prodclass)

Did both and they both throw up ALL distinct classes. Or did I do the group syntax wrong?

jamesw wrote:

This might help make things a little clearer http://guides.rubyonrails.org/active_re … rying.html

I have already read through this guide 10 times (even printed them out and marked everything that was important). Maybe I just don't understand their explanation but in any case I am trying.

Re: What is the best modelstructure and routes?

scope :unique_categories, select(:prodclass).uniq
scope :unique_categories, select(:prodclass).group(:prodclass)

Did both and they both throw up ALL distinct classes. Or did I do the group syntax wrong?

OK, so you are getting somewhere
Using
scope :unique_categories, select(:prodclass).uniq
Open up a rails console in the terminal and type

>> Product.count
...
>> Product.unique_categories.count
...
>> Product.electric.count
...
>> products = Product.electric.unique_categories
...
>> products.size
...
>> products.each do |p|
  >> puts(p.prodclass)
>> end
...

show me the results

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: What is the best modelstructure and routes?

If that gets you what you want then create a new scope called electric_categories but this time make it a class method

  def self.electric_categories
      Product.electric.unique_categories
  end

That will give you a totally unambiguos and easily refactorable set of functions for you to use throughout your app
class method finders like this also act like scopes and can be chained together. (From Rails version 3 onwards)
So you now have
Product.electric
Product.electric_categories
and
Product.motoric

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: What is the best modelstructure and routes?

jamesw wrote:
>> Product.count
...
>> Product.unique_categories.count
...
>> Product.electric.count
...
>> products = Product.electric.unique_categories
...
>> products.size
...
>> products.each do |p|
  >> puts(p.prodclass)
>> end
...

show me the results

Product.count = 5539
Product.unique_categories.count = 332
Product.electric.count = 883
Products.electric.unique_categories = 
[#<Product prodclass: "A">, #<Product prodclass: "C">, #<Product prodclass: "D">, #<Product prodclass: "
F">, #<Product prodclass: "M">, #<Product prodclass: "S">, #<Product prodclass: "V"
>]
products.sizes = 7

It is giving the right amount of classes into the console. I also followed your instructions on setting up a new method in my Product class:

def self.electric_categories
    Product.electric.unique_categories
  end
  
  def self.motoric_categories
    Product.motoric.unique_categories
end

In my controller:

if selectionParams[:electric] == true
          @classes = Product.electric_categories
        else
          @classes = Product.motoric_categories
        end

What I get:

A - B- C- F- D- H- M- S- V

So again all classes...
I will try to change some stuff and see if I did something wrong, but it is strange smile

Re: What is the best modelstructure and routes?

In my console it gives:

Product.electric_categories.count  = 883

So there is still a problem, I will try to fix that

--> Now I get with the code:

Product.unique_categories.count = 5539

------------------------------------------- Edit 1

And my scope is:
scope :unique_categories, select(:prodclass).uniq

Which is logical because every product is distinct. My query was now:

SELECT DISTINCT COUNT(*) FROM `products`

------------------------------------------- Edit 2

But should be:

SELECT DISTINCT 'prodclass' COUNT(*) FROM `products`

Right?

-------------------------------------------- Edit 3

It works to get the different categories again... don't know how I solved it:
Query

SELECT DISTINCT `products`.`prodclass` FROM `products` WHERE (unitnr = 77 OR status like 'BOTH')

Which gives the 7 different categories.

Last edited by ReBa (2012-10-19 03:26:18)

Re: What is the best modelstructure and routes?

I had the problem that in:

if selectionParams[:electric] == true
    @classes = Product.electric_categories
else
    @classes = Product.motoric_categories
end

The == true has to be an integer: "true". So it was normal I didn't get any different amount.
For the rest, I didn't change a lot:

Product.rb:

scope :electric, where("unitnr= ? OR status like ?", 20, "BOTH")
scope :motoric, where("unitnr!= ?", 20)
scope :unique_categories, select(:prodclass).uniq

def self.electric_categories
  Product.electric.unique_categories
end
  
def self.motoric_categories
  Product.motoric.unique_categories
end

But now I get the different categories... so it works!

If I ask some questions about this:

jamesw wrote:

Good, but the session controller seems an odd plae to be posting to, nevermind, if it works then it's good for now and if problems crop up later this can easily be refactored

Do I have to make a new topic so that any person that is having questions about that won't have to read the previous (unrelated) stuff?

My question is: why is that a bad way (or an unusual way) of doing this via the sessions controller?

Re: What is the best modelstructure and routes?

Do I have to make a new topic so that any person that is having questions about that won't have to read the previous (unrelated) stuff?

No, but feel free if you wish smile

My question is: why is that a bad way (or an unusual way) of doing this via the sessions controller?

I'm not sure how you ended up with a sessions controller in the first place.

Controllers are typically named after the relevance of the area they are dealing with. A sessions controller would typically deal with maintenance of the session object. As the session object is typically used to fake state for a loggerd in user a session controller would normally deal with logging a user in and out and redirecting to a more appropriate action after each event for example. After logging a user in via a create action in the session controller that create action would typically redirect to a landing page or the page they were trying to get to before they encountered the login screen.

What seemed odd about using the session controller in your case is that you are showing information other than user log in credentials.

I would have expected a redirect at the very least to some form of landing page, a users home or welcome page which would have the responsibility of rendering any information that you wish to show a user once they have logged in such as user specific menus.

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: What is the best modelstructure and routes?

Yes, but I was thinking that the session index page was a landing page.

So in fact, I have to make a new controller where I will show (when logged in):

<div id="nav"> Home - About - contact </div>

<div id="main">
   <div class="topic">
        <h2>Select the kind of product you want:</h2>
        <%= link_to "Electric", ... %>
        <%= link_to "Motoric", ... %>
   </div>
   <div class="topic">
       <h2>Next topic</h2
       Content
   </div>
</div>

<div class="sidebar right account">
   <h2>Your account:</h2>
   Content
</div>

<div class="sidebar right order">
   <h2>Your order:</h2>
</div>

And for every selection made by the user (when going to the categories and thereafter to the prodgroups) I have to make a new def inside that other controller or not?


Thanks for all help!

Re: What is the best modelstructure and routes?

Yes, but I was thinking that the session index page was a landing page.

So in fact, I have to make a new controller where I will show (when logged in):

Ahh!, no it's just for dealing with logging in and out, so, yes, you should make a new controller to render a landing page once logged in.

And for every selection made by the user (when going to the categories and thereafter to the prodgroups) I have to make a new def inside that other controller or not?

You should design your views with the best user experience in mind. If you do that then, the controllers and actions you needt will become clearer and you will find the controllers and actions you need will just flow.
Focus on the user experience rather than the code, write the code to make the uer experience work rather than making the user experience fit how you want to write the code.

Now for a couple of more difficult topics.
1) The stuff you did in the console to prove that your scopes and methods got you the desired data should be made into a test.
This will have the benefit of you being able to re-run the test every time you change some code and be confident that you haven't stuffed anything up.
There is a really superb, first class, probably the best cast ever done by Ryan Bates that covers testing http://railscasts.com/episodes/275-how-i-test
Did I mention that it's a good cast?

2) The view you have pasted above is not making the best of the Rails framework.
You should be using the master layout to render header, footer and side bar partials

<div id="nav"> Home - About - contact </div>

The above should be a nav bar partial redered in your main layout if cuurent user is logged in and positioned using css.
The same goes for

<div class="sidebar right account">
   <h2>Your account:</h2>
   Content
</div>

<div class="sidebar right order">
   <h2>Your order:</h2>
</div>

They should be partials rendered from your main layout positioned using css

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: What is the best modelstructure and routes?

For layoing out your site using css you can't go far wrong if you use one of the layouts from the total css god that is Matthew James Taylor
http://matthewjamestaylor.com/blog/perf … id-layouts

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)