i’m trying to learn some rails and have been doing great up to now.
i have a has_many/belongs_to relationship. i got the new/edit forms
working with a select box and i confirmed the urischeme_id shows up in
the websites table properly.
the problem is when i try to show the urischeme.scheme in my view. i
keep getting errors. i tried pluralizing, caps, and everything i can
think of!!! what am i doing wrong? i have been struggling with this
for hours.
thanks,
scott
// relevant models
class Website < ActiveRecord::Base
belongs_to :clubs
belongs_to :urischemes
validates_presence_of :name, :address, :urischeme_id
end
class Urischeme < ActiveRecord::Base
has_many :websites
validates_presence_of :scheme
end
// controller
def show
@club = Club.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @club }
end
end
// my view code with comments added for this email
<% unless @club.websites.nil? %>
<% @club.websites.each do |website| %>
<%=h website.address -%> // works fine
<%= debug(website) -%> // no error, shows a proper
urischeme_id
<%= debug(website.urischeme) -%> // undefined method
urischeme' for #<Website:0x477cd5c> <%= debug(website.urischeme.scheme) -%> // undefined method
urischeme’ for #Website:0x4743d90
<% end %>
<% end %>
You haven’t defined an association for urischeme.scheme (the validation
doesn’t count as an association and may be wrong as well).
scheme is a column in my urischeme table (see my migration below), why
would I have to define a seperate association? the tables should be
associated by
Website = belongs_to :urischemes
Urischeme = has_many :websites
as for the validations, I was just playing with them to learn how they
work. they actually seem to work, plus if i delete them, i still get
the same errors as above. so I don’t think they are causing my
problem.
// the migration i used for creating the urischeme table
class CreateUrischemes < ActiveRecord::Migration
def self.up
create_table :urischemes do |t|
t.string :scheme
t.timestamps
end
Urischeme.create(:scheme=>'http://')
Urischeme.create(:scheme=>'https://')
end
def self.down
drop_table :urischemes
end
end
On 9 Jul 2008, at 06:12, skud wrote:
thanks,
scott
// relevant models
class Website < ActiveRecord::Base
belongs_to :clubs
belongs_to :urischemes
<%= debug(website) -%> // no error, shows a proper
urischeme_id
<%= debug(website.urischeme) -%> // undefined method
urischeme' for #<Website:0x477cd5c> <%= debug(website.urischeme.scheme) -%> // undefined method
urischeme’ for #Website:0x4743d90
Your association is belongs_to :urischemes, so it will create a
website.urischemes method, but you’re calling website.urischeme
Your association should be belongs_to :urischeme (a belongs_to is
always singular)
Fred
FINALLY SUCCESS!!!
I knew it would be a simple fix.
thanks