edit related record

Discussion in 'Ruby' started by Thufir, Nov 15, 2009.

  1. Thufir

    Thufir Guest

    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
     
    Thufir, Nov 15, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SnVzdGlu?=

    Retrieving Record Key while creating the record.

    =?Utf-8?B?SnVzdGlu?=, Oct 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    665
    =?Utf-8?B?SnVzdGlu?=
    Oct 5, 2004
  2. André
    Replies:
    0
    Views:
    3,783
    André
    Jun 25, 2006
  3. Maxwell Hammer
    Replies:
    7
    Views:
    673
    Peter Hansen
    Jun 18, 2005
  4. MRW
    Replies:
    0
    Views:
    400
  5. André
    Replies:
    0
    Views:
    467
    André
    Jun 25, 2006
Loading...

Share This Page