Help Updating Database With Checkboxes

Hi,
I have a view that displays a list of items from a database table called
“order_items”. Each item has a checkbox next to it. I need the user to
be able to select multiple items with the checkboxes and have those
items update another database table called “line_items”. Whenever I save
the form, in the “line_item_id” column of the “order_item_id” table I
keep seeing stuff like “-615326988”, which is obviously incorrect. What
is that weird negative number? Why does it put that instead of what I
need?
Below is what I’m working with so far…

Controller:

[CODE]
def create
@order = Order.new(params[:order])
@lineitem = LineItem.new(params[:line_item])
@orderitem = OrderItem.find(:all)

if @order.save
@lineitem.order_id = @order.id
@lineitem.order_item_id = @orderitem.id
if @lineitem.save
LineItem.update_all([“issued_date=?”, Time.now], :id =>
params[:orderitem_ids])
redirect_to :controller=> ‘orders’, :action => ‘viewAllOrders’
else
flash[:warning] = “Error - orders controller - @lineitem not
saved.”
end
else
flash[:warning] = “Error - orders controller - @order not saved.”
end
end[/CODE]

View:

<% form_tag({:action => 'create'}, {:id=> 'page1'}) do %> <% @orderitem.each do |orderitem| %> <%= check_box_tag "orderitem_ids[]", orderitem.id %> <%= orderitem.name %> <% end %> <% end %>

Output:

Processing OrdersController#create (for 127.0.0.1 at 2007-12-26
12:07:42) [POST]
Session ID: 57faab505a5f2807583b841df5d58534
Parameters: {“commit”=>“Create”, “order”=>{“location_id”=>“3”},
“action”=>“create”, “controller”=>“orders”, “orderitem_ids”=>[“1”, “3”],
“line_item”=>{“quantity_requested”=>“4”}}

SQL (0.000104) BEGIN
SQL (0.043289) INSERT INTO line_items (order_id, order_item_id,
quantity_requested, issued_date, price_at_order, parlevel,
quantity_issued) VALUES(80, -615326988, 4, NULL, NULL, NULL, NULL)
SQL (0.001171) COMMIT
LineItem Update (0.000302) UPDATE line_items SET
issued_date=‘2007-12-26 12:07:42’ WHERE (line_items.id IN (‘1’,‘3’))

Generated Page Source:


Item X

I don’t understand your code very well, but i think that you need
something
like this:

http://wiki.rubyonrails.org/rails/pages/CheckboxHABTM

The answer about the negative id is on:

@orderitem = OrderItem.find(:all)

@lineitem.order_item_id = @orderitem.id

@orderitem is an Array!

On Dec 26, 2007 3:31 PM, Mr. Bill [email protected]
wrote:

Below is what I’m working with so far…
@lineitem.order_item_id = @orderitem.id
end

SQL (0.043289) INSERT INTO line_items (order_id, order_item_id,
Item X

Posted via http://www.ruby-forum.com/.


Everton J. Carpes
Mobile: +55 53 9129.4593
MSN: [email protected]
UIN: 343716195
Jabber: [email protected]

“If art interprets our dreams, the computer executes them in the guise
of
programs!” - Alan J. Perlis

Thank you for your reply Everton. I will look into HABTM and HABTM
checkboxes.

I’m still confused about the negative id. Everything I try either
produces the same result or makes the order_item_id NULL. Can you give a
hint on what I should be adding/subtracting from the code? What should
@orderitem be instead of an array?

The answer about the negative id is on:

@orderitem = OrderItem.find(:all)

@lineitem.order_item_id = @orderitem.id

@orderitem is an Array!

On Wed, 26 Dec 2007 20:15:00 +0100, Mr. Bill wrote:

Thank you for your reply Everton. I will look into HABTM and HABTM
checkboxes.

Has many through gives more flexibility, no?

-Thufir

I think that what you need is just a checkbox like:

<%= check_box_tag “line_item[orderitem_ids][]”, orderitem.id %>

And so,

@lineitem = LineItem.new(params[:line_item])
@lineitem.save

will works fine!

P.S.: the problem about the negative Array id, is because the Array id
is
the Ruby object id, not an ActiveRecord id.

On Dec 26, 2007 5:15 PM, Mr. Bill [email protected]
wrote:

The answer about the negative id is on:

@orderitem = OrderItem.find(:all)

@lineitem.order_item_id = @orderitem.id

@orderitem is an Array!

Posted via http://www.ruby-forum.com/.


Everton J. Carpes
Mobile: +55 53 9129.4593
MSN: [email protected]
UIN: 343716195
Jabber: [email protected]

“If art interprets our dreams, the computer executes them in the guise
of
programs!” - Alan J. Perlis

Thufir:

Has many through gives more flexibility, no?

Yes, in this case through is the best solution, but the problem about
creation will be solved at the some way.

Mr. Bill:

Post your models’s source.

On Dec 26, 2007 6:17 PM, Mr. Bill [email protected]
wrote:

And so,


Posted via http://www.ruby-forum.com/.


Everton J. Carpes
Mobile: +55 53 9129.4593
MSN: [email protected]
UIN: 343716195
Jabber: [email protected]

“If art interprets our dreams, the computer executes them in the guise
of
programs!” - Alan J. Perlis

OK, when I try

<%= check_box_tag “line_item[orderitem_ids][]”, orderitem.id %>

it shows the error:

"NoMethodError in OrdersController#create

undefined method `orderitem_ids=’ for #LineItem:0xb69024e4"

When I try

<%= check_box_tag “line_item[orderitem_ids][]”

it shows:

"ActiveRecord::StatementInvalid in OrdersController#create

Mysql::Error: Table ‘databasename.line_items_order_items’ doesn’t exist:
SHOW FIELDS FROM line_items_order_items"

Yes, Thufir, HABTM looks very promising. I haven’t had much time to look
into it yet though.

Everton J. Carpes wrote:

I think that what you need is just a checkbox like:

<%= check_box_tag “line_item[orderitem_ids][]”, orderitem.id %>

And so,

@lineitem = LineItem.new(params[:line_item])
@lineitem.save

will works fine!

P.S.: the problem about the negative Array id, is because the Array id
is
the Ruby object id, not an ActiveRecord id.

Your OrderItem model has not the belongs_to :line_items, but this is not
the
problem.
I can’t understand why method missing, because you have has_many
:order_items…

Well, sory, but i can’t help more.

On Dec 27, 2007 12:15 AM, Mr. Bill [email protected]
wrote:

has_many :order_items
find(:all)


Posted via http://www.ruby-forum.com/.


Everton J. Carpes
Mobile: +55 53 9129.4593
MSN: [email protected]
UIN: 343716195
Jabber: [email protected]

“If art interprets our dreams, the computer executes them in the guise
of
programs!” - Alan J. Perlis

On Dec 26, 6:15 pm, “Mr. Bill” [email protected]
wrote:

end

class LineItem < ActiveRecord::Base
#this model represents one line entry in a given order
belongs_to :order
has_many :order_items
end

For a HABTM (Has and Belongs To Many) I believe that you need to
change the names for tables and classes. It seems contentious, but to
my understanding it would be:

class Order
class Item
class ItemOrder

corresponding to:

table orders
table items
table items_orders

or, perhaps it’s item_orders, or order_items for the table name. That
part seems contentious. Oh, I find “item” to be kinda vague as it
could be any kind of item. How about “dish”, “entree” or “food”?

-Thufir

Everton J. Carpes wrote:

Mr. Bill:

Post your models’s source.

Here are the models I’m working with:

class Order < ActiveRecord::Base
has_many :order_items
has_many :line_items
end

class OrderItem < ActiveRecord::Base
#Order Items are the various available items that can be placed in an
order
belongs_to :order

def self.find_items
find(:all)
end
end

class LineItem < ActiveRecord::Base
#this model represents one line entry in a given order
belongs_to :order
has_many :order_items
end

items_orders is correct, as HABTM join tables need to be in alphabetical
order. Well, apparently. I’ve never tried putting it out of order.


Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

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