@session['user'] vs session[:user]

This might seem really simple to some but just wanted to ask: what is
the difference between @session[‘user’] and session[:user] ? and how
would you use each?

And another question, I have the following code:

def get_customer
if @session[‘customer’]
@c = Customer.find(@session[‘customer’])
end
end

private

def initialize_cart
  if session[:cart_id]
    @cart = Cart.find(session[:cart_id])
  else
    @cart = Cart.create
    session[:cart_id] = @cart.id
  end
end

How do I assign the cart to the logged in customer?

Thanks in advance,
Elle

On 17 Oct 2007, at 07:44, elle wrote:

This might seem really simple to some but just wanted to ask: what is
the difference between @session[‘user’] and session[:user] ? and how
would you use each?

@session is deprecated. Use session (which is a method).

def initialize_cart
  if session[:cart_id]
    @cart = Cart.find(session[:cart_id])
  else
    @cart = Cart.create
    session[:cart_id] = @cart.id
  end
end

How do I assign the cart to the logged in customer?

Not entirely sure what you mean, but you’ve got an @cart containing a
cart and @c containing your customer so you should be able to put the
2 together.

Fred

well ‘customer’ is a string:

irb(main):001:0> ‘customer’.class
=> String

and :customer is a symbol:

irb(main):002:0> :customer.class
=> Symbol

I don’t know much about ruby, but indexing (or what is the right term)
with symbols is better idea then indexing with strings. Because (in-
theory at least) an object of Symbol class is smaller then an object
of a String class. Try ‘customer’.methods vs. :customer.methods

well ‘customer’ is a string:

Some Hashes in Rails are old-fashioned, and some use
.with_indifferent_access. That means you can insert strings or symbols,
and
query symbols or strings, indifferently. This allows you to use the most
natural expressions when inserting or querying.

I don’t know much about ruby, but indexing (or what is the right term)
with symbols is better idea then indexing with strings. Because (in-
theory at least) an object of Symbol class is smaller then an object of a
String class.

Symbols are converted to unique numbers at compile time, while Ruby must
wait until evaluation time to convert strings into numbers. So symbols
are
generally faster.

Try ‘customer’.methods vs. :customer.methods

Cute, but the number of methods is not a direct indicator of speed or
size!

Indirectly, things with fewer methods have more opportunities for
optimization, so a symbol indeed has fewer methods!


Phlip

what is

the difference between @session[‘user’] and session[:user] ?
session[‘user’] when you say, there is a temporary string object
created to access the session hash,

session[:user] when you say, No temporary object created here, but
there is only Symbol object created at the first
time of use. And from then onwards the any access to sessoin[:user]
uses same Symbol object. Meaning there will
be only one memory reference to symbol object.

How do I assign the cart to the logged in customer?
you can do in different ways,

  1. Like what you are trying to implement, Create a new Card Object,
    once a user logs in and assign that card_id to
    the logged in user’s session. Then, from then onwards use that card id
    to add any product which plan to purchase.

  2. Create Some database relation-ship between user and card model.

  3. Use javascript, ajax and cookie to implement your card logic.

OK, so I have the following tables in my db: customers, carts,
cart_items.
The carts table has 2 columns: id and customer_id
The Customer class has: has_many : carts
The Cart class has: has_many : products :through :cart_items
has_one :customer

Also, when the customer logs in, to display his name for example, I
use @c.name in the view. I’m really new to rails but from my
understanding, @c is an array (is that right?) that holds all of the
customer’s details.

I’ve tried to assign the @cart to @c but it didn’t work. I’m not sure
how to do this.
Also now, I call all the cart items by using @cart.products.
How do i cahnge it to call for all the products in the cart that
belongs to customer?

This is the code for customer login and logout:

def login
# examine the form data for “name” and “password”
pw,name = params[:customer].values_at(*%w{password name})
# Retrieve the customer record for the name and store it in a
variable ‘c’
c = Customer.find_by_name(name)
# if such record exists, and it’s password matches the password
from the form
if c && Digest::SHA1.hexdigest(pw) == c.password
# start a session with the customer id
@session[‘customer’] = c.id

    if @cart.products.empty?
        redirect_to :controller => "catalog"
     else
     redirect_to :action => "view_cart"
     end
 else
   # otherwise report an error
   flash[:notice] = "Invalid Login"
   redirect_to :controller => "catalog"
 end

end

def logout
@session[‘customer’] = nil
redirect_to :controller => “catalog”
end

How should I change my code to make this happen?

Thanks,
Elle

Cute, but the number of methods is not a direct indicator of speed or
size!

not precisely - if there are fewer methods the initialization process (
creating a new object ) is usually faster.

sorry, i am not able to understand your schema definition.
what will cart table contain? is it a mapping table?, if yes follow
rails naming convention.
what is the relation ship between cart, cart_items and products. ? and
what does cart_item contain?

usuallly when you have has_many relationship, you can say

@customer.cart_ids = [1,2,3]

where [1,2,3] is list of cart id’s which you want to associate to that
user.
and CustomerModel should have relationship with cart model, with name
cart (name give to has_many).

you can read any any rails book to understand this relationship topic.

Good Luck.,

Thank you everyone so much for assisting me in this matter.

I changed the code so instead of having session[:customer] and
session[:cart], there is only one session[:customer] with the
customer’s id. But now I am getting an error:

NoMethodError in CartController#login
undefined method items' for 129:Fixnum app/controllers/cart_controller.rb:17:inlogin

my cart_controller.rb has the following:

class CartController < ApplicationController
before_filter :authorize, :except => [“login”]

def authorize
return true if @c
flash[:notice] = “Please login again to place your order”
redirect_to :controller => “catalog”
end

def login
pw,name = params[:customer].values_at(*%w{password name})
c = Customer.find_by_name(name)
if c && Digest::SHA1.hexdigest(pw) == c.password
# start a session with the customer id
session[:customer] = c.id
@cart = find_cart
if @cart.items.empty?
redirect_to :controller => “catalog”
else
redirect_to :action => “view_cart”
end
else
# otherwise report an error
flash[:notice] = “Invalid Login”
redirect_to :controller => “catalog”
end
end

def logout
session[:customer] = nil
redirect_to :controller => “catalog”
end
.
.
.
def find_cart
unless session[:customer]
@cart = Cart.new
session[:customer] = @cart
end
session[:customer]
end

cart.rb class:

class Cart < ActiveRecord::Base

has_many :cart_items

has_many :products, :through => :cart_items

belongs_to :customer

attr_reader :items

def initialize
@items = []
end

and lastly, the application.rb:

before_filter :current_customer

def current_customer
if session[:customer]
@c = Customer.find(session[:customer])
end
end

So, my login works but the app doesn’t accept the “if
@cart.items.empty?”
Any ideas?

Thanks,
Elle

On 19 Oct, 12:45, elle [email protected] wrote:

So, my login works but the app doesn’t accept the “if
@cart.items.empty?”
Any ideas?

Looks like you have a model named cart_items, not just items. So, it
should be if @cart.cart_items.empty?

Robin