Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Rails3 & sqlite3 database associations issue

  • 12-03-2011 8: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