# Edit related record

**URL:** <https://www.ruby-forum.com/t/edit-related-record/178998>\
**Category:** Ruby\
**Created:** [November 15, 2009, 10:07am UTC](https://www.ruby-forum.com/t/edit-related-record/178998 "2009-11-15T10:07:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 15, 2009, 10:07am UTC](https://www.ruby-forum.com/t/edit-related-record/178998/1 "2009-11-15T10:07:24Z")

</div>

There’s a one-to-one relation between the base model “item” and the  
associated model “page,” but the page table is empty and the structure  
seems wrong (no fk):

mysql\>  
mysql\> describe rss2mysql.pages;  
±------±--------±-----±----±--------±---------------+  
| Field | Type | Null | Key | Default | Extra |  
±------±--------±-----±----±--------±---------------+  
| id | int(11) | NO | PRI | NULL | auto\_increment |  
| html | text | YES | | NULL | |  
±------±--------±-----±----±--------±---------------+  
2 rows in set (0.00 sec)

mysql\> Aborted  
thufir@ARRAKIS:~/projects/rss2mysql$

It’s necessary to use “through” so that the pages table will hold the fk  
for the id of the “item” to which a given page is associated with?

More importantly, _how_ do you forge the link between a specific item  
and  
the page of which it has\_one? If it already has a page, probably I  
would  
want nothing done. However, if it doesn’t yet have a page, then I want  
to do something like:

page = Page.new  
page = f.readlines.join  
#somehow connect this page to the id of the current item  
page.save

except that every example I see (of a rails controller) is using  
instance  
variables, and, presumably, using rails magic to establish the link  
between objects.

thufir@ARRAKIS:~/projects/rss2mysql$  
thufir@ARRAKIS:~/projects/rss2mysql$ nl item.rb  
1 require ‘rubygems’  
2 require ‘activerecord’

```
 3 class Item < ActiveRecord::Base
 4 has_one :page
 5 end

```

thufir@ARRAKIS:~/projects/rss2mysql$ nl page.rb  
1 require ‘rubygems’  
2 require ‘activerecord’

```
 3 class Page < ActiveRecord::Base
 4 belongs_to :item
 5 end

```

thufir@ARRAKIS:~/projects/rss2mysql$  
thufir@ARRAKIS:~/projects/rss2mysql$ nl scrape.rb  
1 require ‘rubygems’  
2 require ‘activerecord’  
3 require ‘yaml’  
4 require ‘item’  
5 require ‘open-uri’  
6 require ‘pp’

```
 7 db = YAML::load(File.open('database.yml'))

 8 ActiveRecord::Base.establish_connection(
 9 :adapter => db["development"]["adapter"],
10 :host => db["development"]["host"],
11 :username => db["development"]["username"],
12 :password => db["development"]["password"],
13 :database => db["development"]["database"])

14 items = Item.find(:all)

15 items.each do |item|
16 open(item.url,
17 "User-Agent" => "Mozilla/5.0 (X11; U; Linux i686; en-US;

```

rv:1.9.0.15) Gecko/2009102815 Ubuntu/9.04 (jaunty) Firefox/3.0.15"){|f|

```
18 #@item.page = @page ?why use instance vars?

19 item.page.html = f.readlines.join
20 item.save}
21 end

```

thufir@ARRAKIS:~/projects/rss2mysql$

thanks,

Thufir
