# Checkboxes - Saving a Checked record to another table

**URL:** <https://www.ruby-forum.com/t/checkboxes-saving-a-checked-record-to-another-table/58694>\
**Category:** Rails\
**Created:** [April 24, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/checkboxes-saving-a-checked-record-to-another-table/58694 "2006-04-24T22:19:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [April 24, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/checkboxes-saving-a-checked-record-to-another-table/58694/1 "2006-04-24T22:19:47Z")

</div>

I am displaying a bunch of records from a table called “assets”. Next  
to each record i have a checkbox. When this box is checked i want to be  
able to save the id of the record into another table called  
“flagged\_assets”. All I need is the ID, nothing else. Im struggling  
with the logic to save to another table. I know how to do it if the  
asset\_id is already in the “flagged\_assets” table.

# when flag\_assets page is loaded

def flag\_assets  
@assets = Asset.find :all  
end

# Called when Ajax form is submitted

def get\_flagged

to\_flag = params[:to\_be\_flagged]

```
 if to_flag
   to_flag.each do |asset_id, flag|
	 @assets = FlaggedAsset.find_all_by_asset_id(asset_id)
	   for asset in @assets
	    if flag == "yes"
	     asset.flag = 1
	    else
	     asset.flag = 0
	    end
	   asset.save!
     end
  end

```

end  
render :text =\> “

Assets have been Flagged

”  
end

def get\_flagged

to\_flag = params[:to\_be\_flagged]

```
 if to_flag
   to_flag.each do |asset_id, flag|
	 @assets = Test.find_all_by_asset_id(asset_id)
	   for asset in @assets
	    if flag == "yes"
	     asset.flag = 1
	    else
	     asset.flag = 0
	    end
	   asset.save!
     end
  end

```

end  
render :text =\> “

Assets have been Flagged

”  
end

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [April 24, 2006, 10:21pm UTC](https://www.ruby-forum.com/t/checkboxes-saving-a-checked-record-to-another-table/58694/2 "2006-04-24T22:21:34Z")

</div>

Please disregard the second def get\_flagged method. Pasted by accident.

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [April 24, 2006, 10:55pm UTC](https://www.ruby-forum.com/t/checkboxes-saving-a-checked-record-to-another-table/58694/3 "2006-04-24T22:55:50Z")

</div>

Adam wrote:

> I am displaying a bunch of records from a table called “assets”. Next  
> to each record i have a checkbox. When this box is checked i want to be  
> able to save the id of the record into another table called  
> “flagged\_assets”. All I need is the ID, nothing else. Im struggling  
> with the logic to save to another table. I know how to do it if the  
> asset\_id is already in the “flagged\_assets” table.
> 
> # when flag\_assets page is loaded
> 
> def flag\_assets  
> @assets = Asset.find :all  
> end
> 
> # Called when Ajax form is submitted
> 
> def get\_flagged
> 
> to\_flag = params[:to\_be\_flagged]
> 
> ```
> if to_flag
> to_flag.each do |asset_id, flag|
> @assets = FlaggedAsset.find_all_by_asset_id(asset_id)
> for asset in @assets
> if flag == "yes"
> asset.flag = 1
> else
> asset.flag = 0
> end
> asset.save!
> end
> end
> 
> ```
> 
> end  
> render :text =\> “
> 
> Assets have been Flagged
> 
> ”  
> end
> 
> def get\_flagged
> 
> to\_flag = params[:to\_be\_flagged]
> 
> ```
> if to_flag
> to_flag.each do |asset_id, flag|
> @assets = Test.find_all_by_asset_id(asset_id)
> for asset in @assets
> if flag == "yes"
> asset.flag = 1
> else
> asset.flag = 0
> end
> asset.save!
> end
> end
> 
> ```
> 
> end  
> render :text =\> “
> 
> Assets have been Flagged
> 
> ”  
> end  
> Hi Adam,

From what I understand you want to only store the ID of those that are  
checked into a new table. In this case why donâ??t you iterate through  
all the checked boxes in the hash and create a new FlaggedAsset object  
with the ID for each iteration?

By the way, why donâ??t you put a flag column in the Asset table, instead  
of creating a new table?

Bob
