Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > edit related record

Reply
Thread Tools

edit related record

 
 
Thufir
Guest
Posts: n/a
 
      11-15-2009
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 age
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 assword => 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
You cannot add or change a record because a related record is required in table 'lok" André ASP .Net 0 06-25-2006 01:30 PM
You cannot add or change a record because a related record is required in table 'lok" André ASP .Net Web Controls 0 06-25-2006 01:30 PM
How should threads be terminated? (related to 'Help with thread related tracebacks') Maxwell Hammer Python 7 06-18-2005 04:20 PM
Snapshot restraint - edit, edit, edit Alan Browne Digital Photography 24 05-10-2005 10:15 PM
Snapshot restraint - edit, edit, edit Patrick Digital Photography 0 05-06-2005 10:53 PM



Advertisments