Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Rails3 & sqlite3 database associations issue

  • 12-03-2011 09:24PM
    #1
    Registered Users, Registered Users 2 Posts: 7,838 ✭✭✭


    Hi, I really need help with this. I've been at it and google for hours but cant make sense of it...Any Rails geniuses can step forward, nows your time to shine ;)


    I've got a Products, Line_item, Order and User model.

    I want to be able to display the products that are associated with a users order through a line_item. I'm trying to do this in the user controller.

    I've been reading a little about

    The models:
    class Product < ActiveRecord::Base
    		
    	has_many :line_items
    	has_many :orders, :through => :line_items
    
    end
    
    class LineItem < ActiveRecord::Base
    
    	belongs_to :order
    	belongs_to :album
    	belongs_to :basket
    	belongs_to :user
    
    end
    
    class Order < ActiveRecord::Base
    
    	has_many :line_items, :dependent => :destroy
    	has_many :albums, :through => :line_items
    	belongs_to :users
    	
    end
    
    class User < ActiveRecord::Base
      
      has_many :orders
      has_many :line_items, :through => :orders
      has_many :albums
    
    end
    

    Users Controller:
    class UsersController < ApplicationController
    
    .....
    
    def account
        @user = User.find_by_id(session[:user_id]) 
        @orders = @user.orders
    
        [COLOR="brown"]@items = @orders.line_item [/COLOR] [U]# pseudocode: get
     albums whose id appears in a line_item that references an order belonging
     to a particular user_id[/U]
    
        respond_to do |format|
          format.html # show.html.erb
          format.xml  { render :xml => @user }
        end
    end
    
    .....
    end
    

    And the accounts.html.erb view in the User Controller:

    [HTML]<% unless @orders.empty? %>
    <p>
    <b>Your Order History:</b>
    </p>
    <table>
    <tr>
    <td>Date</td>
    <td>Order No.</td>
    <td>Products</td>
    <td>Quantity</td>
    <td>Price</td>
    <td>Total</td>
    </tr>
    <td>


    <% @orders.each do |order| %>
    <tr>
    <td><%= order.created_at %></td>
    <td><%= order.id %></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <% @items.each do |item| %>
    <tr>
    <td></td>
    <td></td>
    <td><%= item.title %></td>
    <td><%= item.price %></td>
    <td><%= item.quantity %></td>
    <td><%= item.total_price %></td>
    </tr>
    <% end %>
    </table>

    <% else %>
    You have made no purchases.
    <% end %>[/HTML]


Comments

  • Registered Users, Registered Users 2 Posts: 7,838 ✭✭✭Nulty


    Starting to think I need a new controller for this. Any one interested in Rails should check out www.railscasts.com


  • Registered Users, Registered Users 2 Posts: 7,838 ✭✭✭Nulty


    I got it sorted. Totally over-complicated things and tied my self up in knots..

    Sorted.

    @itemised_orders = Order.find_all_by_id(@user) is an array of orders. To get the individual values from the array I had to loop though them calling the attributes for each one in the loop. Theres no need to mention the line items because of the
    has_many :orders[B], :through => :line_items[/B]
    

    class UsersController < ApplicationController
    
    .....
    
    def account
        @user = User.find_by_id(session[:user_id]) 
        @orders = @user.orders
        @itemised_orders = Order.find_all_by_id(@user)
    
        [S]@items = @orders.line_item  # pseudocode: get
     albums whose id appears in a line_item that references an order belonging
     to a particular user_id[/S]
    
        respond_to do |format|
          format.html # show.html.erb
          format.xml  { render :xml => @user }
        end
    end
    
    .....
    end
    
      <% @orders.each do |orders| %>
    	 <tr>
    	  <td><%= orders.created_at %></td>
    	  <% orders.line_items.each do |item| %>
            <td><%= item.product.title %> </td>
            <td>&euro;<%= item.product.price %></td>
            <td><%= item.id %></td>
            <% end %>
    	  <td><%= orders.sname %></td>
    	  <td><%= orders.address1 %></td>
    	 </tr>
      <% end %>
      </table>
    


Advertisement