Join and Refer In Tables

OK: I’ve read through this a million times and am still not getting it
right. I have two tables (posts and authors), with a one(author) to
many(posts) relationship, and the keys are not named id but user_name in
one and screen_name in the other. I’m trying to join the tables in the
model, then pull all fields from posts and one additional field from
author, bio. Here is my stuff:

in models/post.rb

class Post < ActiveRecord::Base
has_one :authors, :primary_key => :user_name, :foreign_key =>
:screen_name
end

in controllers/posts.rb

@postswithauthors = Post.find(:all, :limit => 20, :conditions =>“posted
between '”[email protected]+"’ and ‘"[email protected]+"’ and
searchterm=’"+searchterm+"’", :order => “posted DESC”, :include =>
:authors )

in the view/posts.rb

for x in @postswithauthors
puts x.user_name
puts x.posted
puts x.post
puts x.authors.bio
end

Every time I run this I get something like invalid method “bio”. Been
over and over all of the association docs and can’t find my error.
Please help.

Thanks…Chris

“has_one” should only be used in one-to-one relationships.
This is a one-to-many relationship, so use “belongs_to :author,
:foreign_key => :screen_name” in the model, “:include => :author” in the
controller, and “x.author.bio” in the view.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs