Problem calling super constructor

I had a lot of messy controller code that i’m in the process of tidying
up (moving logic into the models for example). Now, this no longer
works and i don’t understand the message “undefined method `article=’
for #Article:0x4518714” - can anyone explain this to me please?

Here’s my stack trace, i’ll go through it step by step afterwards -

C:/code/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1857:in
method_missing' C:/code/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1672:insend’
C:/code/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1672:in
attributes=' C:/code/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1671:ineach’
C:/code/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1671:in
attributes=' C:/code/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1505:ininitialize_without_callbacks’
C:/code/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/callbacks.rb:225:in
initialize' #{RAILS_ROOT}/app/models/article.rb:29:ininitialize’
#{RAILS_ROOT}/app/models/article.rb:48:in new' #{RAILS_ROOT}/app/models/article.rb:48:inadd_article’
#{RAILS_ROOT}/app/controllers/article_controller.rb:35:in `add’

OK - we start off with a form for getting some details for the new
object, an Article:

<% form_tag :controller => ‘article’, :action => ‘add’ do %>

Url
<%= text_field 'article', 'url' %>

Title
<%= text_field 'article', 'title' %>

Tags
<%= text_field 'article', 'tags' %>

<%= submit_tag "Submit" %> <% end %>

The results from this get passed through in a hash called :article to my
controller ‘add’ method, which passes the params through the the model’s
add_article method, along with the current user:

def add
if Article.add_article :article => params[:article], :user_id =>
session[:user]
flash[:notice] = “Added a new article.”
else
flash[:notice] = “This article exists already. It has been
automatically scored up for you.”
end
redirect_to :controller => ‘article’, :action => ‘list’
end

In the model’s add_article method, i see if an article with the same url
is already in the DB, and if it isn’t i call the constructor, just
passing through the same params, unchanged:

def Article.add_article(params = {})
#see if we’ve already got it - if not, make a new one, if we do,
score it up
article = Article.find(:first, :conditions => [“url = ?”,
params[:article][:url]])
if article == nil
Article.new params
return true
else
article.change_score :user_id => params[:user_id], :points => 1
return false
end
end

And this is the constructor, which takes the params, which include the
:article hash and the :user_id value. I call super before anything else
to make sure i get an ActiveRecord object. (the hpricot code is just
used to autogenerate a title from a url)

def initialize(params = {})
super
if self.title == “” && self.url != “”
self.title = (Hpricot(open(self.url))/“title”).first.inner_html
end
self.added_at = DateTime.now.to_s
self.points = 0
self.change_score :user_id => self.user_id, :points => 1
self.user_id = params[:user_id]
end

Looking at the trace, it looks like the call to super is causing the
problem (line 29 is the super call line) - but this is unchanged from
how it was working before. Can anyone see my problem?

thanks!
max

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