SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A tale of two tables

    I am just starting out in ror and I need a bit of help with two tables. I have a person and residence table with a residence_id field in the person table. I have the has_many, belong_to methods in the appropriate person and residence methds. What I want to do is list all the fields from both the person and corresponding address tables. I can list all the person data but I can't seem to list the residence data. Can someone give me an example of how to do thad so that I can display data such as person.streetname and person.housenum in a list view. I know it has to be easy but I have exhausted all the examples I have without success.

    Rick

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well lets say your belongs_to looks like this in your Person model:
    Code:
    belongs_to :residence
    You access it like this:
    Code:
    my_person = Person.find(:first, :include => :residence)
    my_person.residence.streetname
    my_person.residence.housenum
    That is the only way to access it by default. To access it like my_person.streetname you could always do this:

    Code:
    class Person < ActiveRecord::Base
      ...
    
      def streetname
    	self.residence.streetname
      end
    end
    And so on. In Rails 1.1, which should be out sometime soon, you can use delegations to make life easier, example:

    Code:
    class Person < ActiveRecord::Base
      belongs_to :residence
      delegate :streetname, :to => 'residence'
      delegate :housenum, :to => 'residence'
     end
    
    person = Person.find(:first, :include => :residence)
    person.housenum # same thing as: person.residence.housenum
    person.streetname # same thing as: person.residence.streetname
    But don't forget, the above only works in RAILS 1.1, which is not out yet, but should be soon. But what I posted at the top of this post should hold you over.
    I <3 Rails

  3. #3
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that - I had the suspision that it had to be something simple. I amended your code to :
    Code:
     my_person = Person.find(:all, :include => :residence)
    and it worked fine except for the paging. In general, the scaffold produces the following code:
    Code:
     @person_pages, @persons = paginate :persons, :order_by => 'lastname, firstname', :per_page => 10
    . How do I split that statement so I have pagination and the snippet of code you sent me or can I do the whole works in one statement much like that produced by scaffold.

    Rick


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
  •