SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict jkassemi's Avatar
    Join Date
    Jan 2005
    Location
    Albuquerque
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Automatically creating associated records < ActiveRecord RoR

    Hey everyone, I've got an ActiveRecord class like this:

    Code:
    class TestRecord < ActiveRecord::Base
         has_one :topic, :class_name => "Topics", :foreign_key => "id"
         def before_save
              topic = create_topic :name => "mytest"
         end
    end
    
    class Topics < ActiveRecord::Base
         set_table_name 'topics'
    end
    However, when I create a new TestRecord, it inserts a NULL into the topic column... I've tried a few different variations of the above, without success... Can someone help me with this? Is there a different approach I should be taking? It took me a while to "get" the proper approach to a problem, so I'll admit I may not yet have it.


    J

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ehm your code looks weird. What are you trying to do?

  3. #3
    SitePoint Member
    Join Date
    Nov 2006
    Location
    Austin, TX
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have a lot wrong with your code.. So you want a topic created every single time a TestRecord is saved? I don't think that's what you want.

    Code:
    class TestRecord < ActiveRecord::Base
         has_one :topic
         before_create :create_topic
    
         def create_topic
              topic = Topic.create(:name => "mytest")
         end
    end
    
    class Topics < ActiveRecord::Base
    end
    will work, but there's still something very wrong with whatever you are trying to do. Please explain more.
    wtf242
    ProgrammingBooks.org - Programming Books Ranked by Programmers
    Turn of the Crank - My blog
    Robot Walrus - My Art Prints/Posters Blog

  4. #4
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wtf242's suggestions regarding the class name are good, it looks like you could eliminate a lot of configuration code by using a singular class name:
    Code:
    class Topic < ActiveRecord::Base
    I think your main problem is this
    Code:
    topic = whatever
    It's ambiguous, are you creating a local variable named topic or using the instance method "topic=" ? Ruby assumes the former unless you ...
    Code:
    self.topic = whatever
    Additionally, you may need to save again after adding that topic. I can never remember how ActiveRecord does that without testing it.
    Code:
    self.topic = whatever
    self.save 
    # or maybe ...
    topic.save
    # or maybe neither are needed :P
    Notice when you refer to topic there is no ambiguity so you don't need self.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •