Connecting 2 products tables to one cart--anyone please!

hi

ive followed along the depot book. i have a products table, cart,
line_items. All works fine there with adding the products to the cart
etc.

now ive added another table user_products table. i want the user to be
able to add these items to the cart also. but i dunno how to get this
working. i tried to add in user_product code anywhere i saw code for the
product table in the cart and line_item. also added that user_products
belong to line_items too in the model like what is used for the products
table.

i added an extra column in line_item table for user_product_id.

still cant get it working. any insight would be greatly appreciated need
to get this fixed asap. can this be done…the cart taking in items from
the products table and user_products table. Ive tried to find
information on the web but to no avail…if anyone can point me to some
info that would be great.

On Wednesday, May 25, 2011 10:48:57 AM UTC-6, Ruby-Forum.com User wrote:

product table in the cart and line_item. also added that user_products
belong to line_items too in the model like what is used for the products
table.

i added an extra column in line_item table for user_product_id.

still cant get it working.

In what way doesn’t it work? What is the error message and stack trace
returned when it fails?

Also, if you want help here you’re probably going to have to post some
actual code, since just going off your description of what you did
really
tells us close to nothing. Debugging takes concrete data.

On 25 May 2011 17:48, Caroline M. [email protected] wrote:

belong to line_items too in the model like what is used for the products
table.

What are the columns in the products table and in the user_products
table?

Colin

Colin L. wrote in post #1001031:

On 25 May 2011 17:48, Caroline M. [email protected] wrote:

belong to line_items too in the model like what is used for the products
table.

What are the columns in the products table and in the user_products
table?

Colin

Colin,

user_products table has photo, title, message, price, id.

products table has title, price, category, image_url, and id.

thanks

On 25 May 2011 22:05, Caroline M. [email protected] wrote:

Colin,

user_products table has photo, title, message, price, id.

products table has title, price, category, image_url, and id.

Would it be possible to combine the two tables (so that the user
products and products are in the same table)? Either using STI or
maybe just have some way of knowing what sort each product is. Then
from the point of view of your cart life would be easy.

Colin

Colin L. wrote in post #1001149:

On 25 May 2011 22:05, Caroline M. [email protected] wrote:

Colin,

user_products table has photo, title, message, price, id.

products table has title, price, category, image_url, and id.

Would it be possible to combine the two tables (so that the user
products and products are in the same table)? Either using STI or
maybe just have some way of knowing what sort each product is. Then
from the point of view of your cart life would be easy.

Colin

thanks colin, i was trying to think about this already but not sure
really what way to do it. im new to all this. i need the two separate
controllers for the tables because i want users to be able to use the
new/user_products path creating their own products, while on the
products controller the new path is not visible to users. i prob also
need to add user_id to the user_products table. im not sure how to
combine the tables. im tryna fins stuff on net on STI but im not really
understanding it too well.

Kendall G. wrote in post #1001028:

On Wednesday, May 25, 2011 10:48:57 AM UTC-6, Ruby-Forum.com User wrote:

product table in the cart and line_item. also added that user_products
belong to line_items too in the model like what is used for the products
table.

i added an extra column in line_item table for user_product_id.

still cant get it working.

In what way doesn’t it work? What is the error message and stack trace
returned when it fails?

Also, if you want help here you’re probably going to have to post some
actual code, since just going off your description of what you did
really
tells us close to nothing. Debugging takes concrete data.

Sorry just dunno what exactly i should be posting as it seems bits are
needed in alot of places. i’ll try and explain the basics.

my _line_item.html.erb file is as follows:

<% if line_item == @current_item %>

<% else %> <% end %> <%= line_item.quantity %>× <%= line_item.product.title %> <%= number_to_currency(line_item.total_price) %>

that takes in the information from the products table and displays said
product in the cart. my new table is user_products and i want if one of
these products chose to be displayed in cart also. im new to all this so
quite lost. i tried adding “

<%= user_product.title %>” under
the line_item.product.title but it wouldnt run the page and returned an

NameError…undefined local variable or method `user_product’ for #.

basically i dont know how to add in the new instance of a
user_product…should i be using if statement etc.

i have an add to cart button for user_products:
<%= button_to ‘Add to Cart’, line_items_path(:user_product_id =>
user_product),
:remote => true %>

but it says user_product is undefined.

in my line items controller i added user_products lines from what was
already there for product:

def create
@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product.id)
user_product = UserProduct.find(params[:user_product_id])
@line_item = @cart.add_user_product(user_product.id)

respond_to do |format|
  if @line_item.save
    format.html { redirect_to(store_url) }
    format.js   { @current_item = @line_item }
    format.xml  { render :xml => @line_item,
      :status => :created, :location => @line_item }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @line_item.errors,
      :status => :unprocessable_entity }
  end
end

end

hope this is understandable. for user_products it was created using
scaffold so have separate controller,models etc then product.

On 26 May 2011 11:38, Caroline M. [email protected] wrote:

maybe just have some way of knowing what sort each product is. Then
from the point of view of your cart life would be easy.

Colin

thanks colin, i was trying to think about this already but not sure
really what way to do it. im new to all this. i need the two separate
controllers for the tables because i want users to be able to use the
new/user_products path creating their own products, while on the
products controller the new path is not visible to users.

You don’t have to have separate models in order to have two
controllers, both controllers can access the same model. Possibly the
simplest way, to avoid the complications of STI, would just be to put
all the columns you need for both types of product in one table and
maybe use a special entry in the category table to say that is is a
user model. Or alternatively a new column to indicate the type (don’t
call it ‘type’ though or rails will think you are using STI). Then
just leave the unused columns empty.

i prob also
need to add user_id to the user_products table. im not sure how to
combine the tables. im tryna fins stuff on net on STI but im not really
understanding it too well.

Again this will be much easier if you stick to one table. I presume
you know about ActiveRecord relationships, if not have a look at the
Rails Guide on relationships. You can say Product belongs to User and
User has many Products. This will require a user_id column in the
products table. Then you could use the fact that the user_id is nil
to indicate that it is an ordinary product rather than a user product,
rather than use a special category. I am sure you know that then if
you have a user in @user then @user.products will give you all his
products.

I am assuming here that user products are different products to normal
ones (rather than just being ‘ordinary’ products that relate in some
way to the user). If they are actually just the same products then
you would want a different setup altogether.

Colin

Colin L. wrote in post #1001188:

On 26 May 2011 11:38, Caroline M. [email protected] wrote:

maybe just have some way of knowing what sort each product is. Then
from the point of view of your cart life would be easy.

Colin

thanks colin, i was trying to think about this already but not sure
really what way to do it. im new to all this. i need the two separate
controllers for the tables because i want users to be able to use the
new/user_products path creating their own products, while on the
products controller the new path is not visible to users.

You don’t have to have separate models in order to have two
controllers, both controllers can access the same model. Possibly the
simplest way, to avoid the complications of STI, would just be to put
all the columns you need for both types of product in one table and
maybe use a special entry in the category table to say that is is a
user model. Or alternatively a new column to indicate the type (don’t
call it ‘type’ though or rails will think you are using STI). Then
just leave the unused columns empty.

i prob also
need to add user_id to the user_products table. im not sure how to
combine the tables. im tryna fins stuff on net on STI but im not really
understanding it too well.

Again this will be much easier if you stick to one table. I presume
you know about ActiveRecord relationships, if not have a look at the
Rails Guide on relationships. You can say Product belongs to User and
User has many Products. This will require a user_id column in the
products table. Then you could use the fact that the user_id is nil
to indicate that it is an ordinary product rather than a user product,
rather than use a special category. I am sure you know that then if
you have a user in @user then @user.products will give you all his
products.

I am assuming here that user products are different products to normal
ones (rather than just being ‘ordinary’ products that relate in some
way to the user). If they are actually just the same products then
you would want a different setup altogether.

Colin

Colin thanks so much for your help.

the user products are products were the user can upload a photo using
paperclip creating their own personalized products.

so if im understanding correctly your saying to get rid of user_products
table and add the fields for this table into the products table.
and that i still have a products controller and user_products controller
for the views to create the different products…however wont the
user_products controller create them as user_products, not products? and
not be read into the cart then.

Then you could use the fact that the user_id is nil
to indicate that it is an ordinary product rather than a user product,
rather than use a special category.

where would i use this?

im kinda understanding what you mean but just wondering about the
users_products controller and the linking of it to the products
table…act i guess thats fairly easy just changing instances in the
controller from user_product.price to product.price. However for the
user products i want a defined price same for all, but users cant add
this in, would i do this in the controller @save part, some code, price
= etc.? so it would automatically be added into the price field on
creation of product.

i’ll have a link on home page that will connect directly to user
products controller but i need some code also then that will read logged
in users id into the user_id field in database on save too.

On 26 May 2011 14:51, Caroline M. [email protected] wrote:

new/user_products path creating their own products, while on the

to indicate that it is an ordinary product rather than a user product,

user_products controller create them as user_products, not products? and
not be read into the cart then.

There is no direct relationship between a controller and a model. The
type is up to you. For example, if in user_products controller create
action you say
@product = Product.new( params[:product])
then it will make a new Product. The fact that the controller happens
to be called user_products_controller is irrelevant to the model it
manages. A froglets_controller can manage Products if you want it to.

Then you could use the fact that the user_id is nil
to indicate that it is an ordinary product rather than a user product,
rather than use a special category.

where would i use this?

In the controller or view to select which products you wish to view.
Or any other time you wish to select products that are not
user_products.

Have you worked through the free-to-use-online tutorial
railstutorial.org? Although it may not address your exact requirement
it will introduce you to many useful techniques which will give you
ideas on how to go about things.

Colin

Colin L. wrote in post #1001240:

On 26 May 2011 14:51, Caroline M. [email protected] wrote:

new/user_products path creating their own products, while on the

to indicate that it is an ordinary product rather than a user product,

user_products controller create them as user_products, not products? and
not be read into the cart then.

There is no direct relationship between a controller and a model. The
type is up to you. For example, if in user_products controller create
action you say
@product = Product.new( params[:product])
then it will make a new Product. The fact that the controller happens
to be called user_products_controller is irrelevant to the model it
manages. A froglets_controller can manage Products if you want it to.

Then you could use the fact that the user_id is nil
to indicate that it is an ordinary product rather than a user product,
rather than use a special category.

where would i use this?

In the controller or view to select which products you wish to view.
Or any other time you wish to select products that are not
user_products.

Have you worked through the free-to-use-online tutorial
railstutorial.org? Although it may not address your exact requirement
it will introduce you to many useful techniques which will give you
ideas on how to go about things.

Colin

Colin,

i tried to implement what you said i have all the fields now in one
table. im still having a few hiccups with the views and some actions.

in products index i want the products created by user_id 1 only to
display. user_id 1 is my admin user.

on the user_products controller in the index view i want the products
of the logged in user only to display, maybe if user_id 1 is logged in
they could see all products created by other users.

also when i create a product in the user_product new view after i press
create it reverts the product back to the products index instead of
keeping it in an user_products index view, which is what i want as i
have different fields on display for the two different types. so if i
click show etc it displays products show view page but what i want for
that product is the user_product show view page.

also is there a way of the user_id been automatically filled in on save
taking from who is logged in? i also wanted similiar thing on the
user_products side for price, i want to filter a set price for those
products. but dunno if this can be done wen other products prices are
wrote in by admin user and can differ. maybe code saying if the user_id
isnt 1 that the set price is saved on create?

dunno if im explainin this too well. thanks again for your help.

On 26 May 2011 23:12, Caroline M. [email protected] wrote:


Colin,

i tried to implement what you said i have all the fields now in one
table. im still having a few hiccups with the views and some actions.

in products index i want the products created by user_id 1 only to
display. user_id 1 is my admin user.

In the controller fetch the records where that is the id.
@products = Product.where( :user_id => 1)
or if you actually have the admin user available as an object then
@products = admin_user.products
which is much nicer.

on the user_products controller in the index view i want the products
of the logged in user only to display,

In this case
@products = current_user.products

maybe if user_id 1 is logged in
they could see all products created by other users.

If User has a method admin? that tells you whether he is the admin
user (much better than relying on id)

@products = current_user.admin? ? Product.all : current_user.products

also when i create a product in the user_product new view after i press
create it reverts the product back to the products index instead of
keeping it in an user_products index view, which is what i want as i
have different fields on display for the two different types. so if i
click show etc it displays products show view page but what i want for
that product is the user_product show view page.

Where it goes after the create action is up to the code you have
written. After the save call you probably have a redirect_to
statement which tells the browser which page to fetch next. In each
controller specify where you want it to go.

I think in an earlier post I suggested working through
railstutorial.org. This would really be a good idea, at some point
you will look back on these questions and be embarrassed that you had
to ask them. :slight_smile:

Colin

Colin L. wrote in post #1001554:

On 26 May 2011 23:12, Caroline M. [email protected] wrote:

In the controller fetch the records where that is the id.
@products = Product.where( :user_id => 1)

oh thank you so much it worked!!! knew it was along this lines but i
hadn’t the syntax right n hadn’t the .where either.

on the user_products controller in the index view i want the products
of the logged in user only to display,

In this case
@products = current_user.products

again brilliant, im act cant believe where my head is…above statement
fairly obvious, think my head is so frazzled with ruby im not seeing
common sense.

Where it goes after the create action is up to the code you have
written. After the save call you probably have a redirect_to
statement which tells the browser which page to fetch next. In each
controller specify where you want it to go.

now that you point it out, im like of course, duh, ha. il have a play
around with it and see, it was the same as the products controller so
obv it was going there.

I think in an earlier post I suggested working through
railstutorial.org.
thanks for that will have a look, hopefully help me avoid stupid
pitfalls in the future.

you will look back on these questions and be embarrassed that you had
to ask them. :slight_smile:
already there ha

now surprise surprise ive encountered another problem after i did above
changes. ok so my index view now works perfectly in user_products
controller.but when i click on create new product an error pops up. it
was working fine, so dunno why a problem is caused now.unless ive done
something stupid again. ive put whats showing up in the attached pdf.
not sure what else to show you…i tried changing @product to
@current_user.product but that didn’t help. :confused:
thanks for your time and patience!!

On 27 May 2011 18:24, Caroline M. [email protected] wrote:

Attachments:
http://www.ruby-forum.com/attachment/6246/error.pdf

You could just have pasted it all here. Including the complete error
and stack frame is ok.

undefined method `model_name’ for NilClass:Class
Extracted source (around line #1):
1: <%= form_for(@product, :html => { :multipart => true }) do |f| %>

The clue is in the message, it is saying something is nil and the code
is asking for a method to be called on it. The problem is probably
that @product is nil. Assuming that @product is setup by a find call
on the database then it did not find a matching record for some reason
(or perhaps the code does not include a find at all).

As well as working through the tutorial have a look at the Rails Guide
on debugging. It will show you how to use ruby-debug to break into
your code and inspect data and follow flow, which can be invaluable if
you cannot see the error by inspection. Also have a look at
log/development.log in your app, it contains details of the actions
called and the parameters and also the database queries called which
can be invaluable when something is not working.

Colin

Colin L. wrote in post #1001606:

On 27 May 2011 18:24, Caroline M. [email protected] wrote:

Attachments:
http://www.ruby-forum.com/attachment/6246/error.pdf

You could just have pasted it all here. Including the complete error
and stack frame is ok.

undefined method `model_name’ for NilClass:Class
Extracted source (around line #1):
1: <%= form_for(@product, :html => { :multipart => true }) do |f| %>

The clue is in the message, it is saying something is nil and the code
is asking for a method to be called on it. The problem is probably
that @product is nil. Assuming that @product is setup by a find call
on the database then it did not find a matching record for some reason
(or perhaps the code does not include a find at all).

As well as working through the tutorial have a look at the Rails Guide
on debugging. It will show you how to use ruby-debug to break into
your code and inspect data and follow flow, which can be invaluable if
you cannot see the error by inspection. Also have a look at
log/development.log in your app, it contains details of the actions
called and the parameters and also the database queries called which
can be invaluable when something is not working.

Colin

i know its to do with @product= nil but to be honest these errors are a
mindfield to me. im using rails 3. i changed the line from

<%= form_for(@product, :html => { :multipart => true }) do |f| %>

to

<%= form_for :product, @product, :url=>{:action => “create”}, :html => {
:multipart => true } do |f| %>

and removed the following lines:

<% if @product.errors.any? %>


<%= pluralize(@product.errors.count, “error”) %> prohibited
this product from being saved:

  <ul>
  <% @product.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>

<% end %>

then the page rendered and i could fill it it but it would not create
the product and said that there was no create action in userproducts
controller when there clearly is.

i also tried form_tag but again when i hit submit button it would
basically just refresh the page displaying the new product page unfilled
again. i checked db and no products were created. the create function
hasn’t been changed so i don’t see were the problem is.

i looked at log file but not really getting anything from it.

Started GET “/user_products/new” for 127.0.0.1 at Sat May 28 16:30:40
+0100 2011
Processing by UserProductsController#new as HTML
[1m[36mUser Load (1.0ms)[0m [1mSELECT “users”.* FROM “users” WHERE
“users”.“id” = 2 LIMIT 1[0m
Rendered user_products/_form.html.erb (4.0ms)
Rendered user_products/new.html.erb within layouts/application (30.0ms)
Completed in 205ms

ActionView::Template::Error (undefined method model_name' for NilClass:Class): 1: 2: 3: <%= form_for(@product, :html => { :multipart => true }) do |f| %> 4: <% if @product.errors.any? %> 5: <div id="error_explanation"> 6: <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2> app/views/user_products/_form.html.erb:3:in_app_views_user_products__form_html_erb__559743560_50255952_524374’
app/views/user_products/new.html.erb:3:in
`_app_views_user_products_new_html_erb__658334794_50262000_0’

Rendered
C:/Ruby187/lib/ruby/gems/1.8/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/_trace.erb
(4.0ms)
Rendered
C:/Ruby187/lib/ruby/gems/1.8/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb
(17.0ms)
Rendered
C:/Ruby187/lib/ruby/gems/1.8/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/template_error.erb
within rescues/layout (70.0ms)

this is wrecking my head. ive spent hours tryna fix it but maybe as
usual im missing the obvious. any help appreciated.

anyone? still stuck on this :frowning:

problem solved! found a different approach to what i wanted to do, and
made things alot easier for myself.

thanks for all your help.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs