Topic: Please help on linking 4 models...~ thx!

I have faced a very confusing problem(may be it is very easy, but not to me~^^ )..Hope someone can help

I have 4 table..
Product Table
User Table
Orders Table
Line_Items Table

What i want to do is to have a page let the user to see their orders details.

I have searched the web for the whole day about join tables and sth like has many through things..but It just makes me more confused...So, i am wondering if someone would kindly explain a bit to me..

What I am thinking is
1. after a user logined, I get the user email from the user table.
2. I use that email to find all the id in the order table of that email address.
3. Which those ids , each id will match with the "Product_id" in the Line_Items table.
4. And for each Product_id, it will match with the "id" in the "product table" to fetch the name of the product.

as there are 4 models,  here below is what I think of the relations

User
has_many : orders

Order
belongs_to :user
has_many :line_items

Line_Item
has_many : orders
has_many : products

Product
belongs_to :line_item

How can I link that 4 model to acheive what I want to do...

I know this is complicated and long...but please help.... I really tried effort in googling these things and read few times" the Agile Web Development with rails"...so..hope someone can help!~~

Thanks!!!!

Re: Please help on linking 4 models...~ thx!

Hi,

I'll try to give you some hints on your way to the solution.

20dreamer wrote:

I have faced a very confusing problem(may be it is very easy, but not to me~^^ )..Hope someone can help

I have 4 table..
Product Table
User Table
Orders Table
Line_Items Table

When I start something new I try not to think about tables but more on models. I then take a white piece of paper and put some boxes on with the models i probably need. in a second step i draw some lines between them and try to get a picture of the relationships ( 1 to many, 1 to 1, many to many). after that go to rails and create the models, add the foreign keys in migration files for the tables that have a belongs_to.

20dreamer wrote:

What i want to do is to have a page let the user to see their orders details.

I have searched the web for the whole day about join tables and sth like has many through things..but It just makes me more confused...So, i am wondering if someone would kindly explain a bit to me..

What I am thinking is
1. after a user logined, I get the user email from the user table.
2. I use that email to find all the id in the order table of that email address.

When a user logs in to your application he normally does that with username and password or email and password. in both ways your application will give you the user object back (like one row from the users table) all relations are the based on the id of the user and not on the email address

20dreamer wrote:

3. Which those ids , each id will match with the "Product_id" in the Line_Items table.
4. And for each Product_id, it will match with the "id" in the "product table" to fetch the name of the product.

as there are 4 models,  here below is what I think of the relations

User
has_many : orders

Order
belongs_to :user
has_many :line_items

Line_Item
has_many : orders
has_many : products

Product
belongs_to :line_item

I see it like that:

User
has_many : orders

Order
belongs_to :user
has_many :line_items
# to access the products directly from the order model:
has_many :products, :through => :line_items

Line_Item
belongs_to : orders
belongs_to : products

Product
has_many :line_items


so you could access the different models/lists with following methods:
User.orders  #gives a list of all orders for the user
Order.line_items  #gives all items in the order
Order.products  #gives a list of all products in the order

20dreamer wrote:

How can I link that 4 model to acheive what I want to do...

I know this is complicated and long...but please help.... I really tried effort in googling these things and read few times" the Agile Web Development with rails"...so..hope someone can help!~~

Thanks!!!!

hope this helps a bit..

regards,
maze

Re: Please help on linking 4 models...~ thx!

Carefull, it should be:

LineItem
belongs_to :order
belongs_to :product

And as Maze indicated, put a foreign key in the tables with "belongs_to".
For example in table "line_items", you will have a column "order_id" and "product_id"

have fun,
Stijn

Re: Please help on linking 4 models...~ thx!

THANKS!!!
I have makes the associations,
and spint, I have  those column in "line_items" table.

I am able to access the orders with the user_id
which

<% @user.orders.each do |o| %>
<%= o.email %>
<%= o.address %>
<br/>
<% end %>

it shows the correct informations
But when I add
<%= o.products.title %>

I get "undefined method `title' for Product:Class"

any clues?

Thanks!

Re: Please help on linking 4 models...~ thx!

o.products returns a collection of the products of your order, so you have to do the following to list all your products:

<% for p in o.products do %>
<%= p.title %><br />
<% end %>

Regards,

Stijn