# Scusate, dove sbaglio?

**URL:** <https://www.ruby-forum.com/t/scusate-dove-sbaglio/99121>\
**Category:** IT\
**Created:** [July 20, 2006, 12:36pm UTC](https://www.ruby-forum.com/t/scusate-dove-sbaglio/99121 "2006-07-20T12:36:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AGAGAG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@AGAGAG](https://www.ruby-forum.com/u/AGAGAG)\
**Post date:** [July 20, 2006, 12:36pm UTC](https://www.ruby-forum.com/t/scusate-dove-sbaglio/99121/1 "2006-07-20T12:36:28Z")

</div>

Non riesco a visualizzare dopo Posted by: il nome dell’utente che ha  
creato l’articolo, al massimo solo l’id relativo…  
In cosa sbaglio? E’ un problema di relazioni tra tabelle?

- L’errore Ã¨ questo: \*

NoMethodError in Editoriale#index

Showing app/views/editoriale/\_article.rhtml where line #3 raised:

You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.username

Extracted source (around line #3):

1:

  
2: 
# \<%= link\_to article.title, :action =\> ‘showNew’, :id =\> article %\>
  
3: 

Posted by \<%= article.user.username %\> \<%= distance\_of\_time\_in\_words(Time.now,article.created\_at)  
%\> ago

  
4: \<%= debug @article %\>   
5: 
\<%= article.summary %\>
  
6: 

Posted in \<%= article.category.name %\> | \<%=  
link\_to “#{article.comments.count} comments”, :action =\> ‘showNew’, :id  
=\> article %\>

- Queste sono le due tabelle (schema.rb): \*

create\_table “users”, :force =\> true do |t|  
t.column “username”, :string, :limit =\> 30, :default =\> “”, :null =\>  
false  
t.column “name”, :string, :limit =\> 30, :default =\> “”, :null =\>  
false  
t.column “surname”, :string, :limit =\> 30, :default =\> “”, :null =\>  
false  
t.column “email”, :string, :limit =\> 30, :default =\> “”, :null =\>  
false  
t.column “hashed\_password”, :string, :limit =\> 50  
t.column “age”, :integer  
t.column “sex”, :string, :limit =\> 1  
t.column “note”, :text  
t.column “role”, :string, :limit =\> 10, :default =\> “writer”, :null  
=\> false  
end

create\_table “articles”, :force =\> true do |t|  
t.column “id\_prev\_article”, :integer, :default =\> 0  
t.column “author\_id”, :integer, :default =\> 0, :null =\> false  
t.column “created\_at”, :datetime, :null =\> false  
t.column “release\_at”, :datetime, :null =\> false  
t.column “expiration\_at”, :datetime, :null =\> false  
t.column “title”, :text, :default =\> “”, :null =\> false  
t.column “summary”, :text, :default =\> “”, :null =\> false  
t.column “body”, :binary, :default =\> “”, :null =\> false  
t.column “category\_id”, :integer  
t.column “editorial\_status”, :string, :limit =\> 10  
t.column “editor\_id”, :integer  
t.column “editorial\_status\_date”, :datetime  
end

- E questi i due modelli: \*  
class Article \< ActiveRecord::Base  
belongs\_to :user, :class\_name =\> ‘User’, :foreign\_key =\> “author\_id”,  
:foreign\_key =\> “editor\_id”

belongs\_to :category  
has\_many :comments

class User \< ActiveRecord::Base  
has\_many :articles  
attr\_protected :role  
attr\_accessor :password, :password\_confirmation;  
#validates\_length\_of :password, :minimum =\> 5, :message =\> “should be  
at least 5 characters long”  
validates\_presence\_of :name, :username  
validates\_uniqueness\_of :username, :message =\> “already exists”  
validates\_numericality\_of :age, :message =\> “Invalid format of age”  
before\_destroy :dont\_destroy\_admin  
validates\_format\_of :email,  
:with =\>  
/^([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,3})$/,  
:message =\> ‘must be a valid email address’

- Con questa view non riesco in alcun modo a visualizzare il nome  
dell’autore dell’articolo --\> /\_article.rhtml: \*

# \<%= link\_to article.title, :action =\> 'showNew', :id =\> article %\>

Posted by \<%= article.user.username %\> \<%= distance\_of\_time\_in\_words(Time.now,article.created\_at) %\> ago

\<%= debug @article %\> 
\<%= article.summary %\>

Posted in \<%= article.category.name %\> | \<%= link\_to "#{article.comments.count} comments", :action =\> 'showNew', :id =\> article %\>

- Ecco il controller (editoriale\_controller.rb): \*

def list\_published  
@categories = Category.find(:all, :order =\> ‘id asc’, :limit =\> 5)  
@articles\_pages, @articles = paginate(:articles, :conditions =\>  
“editorial\_status = ‘approved’”, :order =\> ‘id desc’)  
end

Vi ringrazio in anticipo.

Alessandro

---

<div class="post-metadata">

**Author:** ![AGAGAG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@AGAGAG](https://www.ruby-forum.com/u/AGAGAG)\
**Post date:** [July 23, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/scusate-dove-sbaglio/99121/2 "2006-07-23T17:05:20Z")

</div>

> Extracted source (around line #3):  
> link\_to “#{article.comments.count} comments”, :action =\> ‘showNew’, :id  
> =\> article %\>
> 
> …  
> …

def list\_published

> * * *
> 
> Ml mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.ruby-it.org/mailman/listinfo/ml](http://lists.ruby-it.org/mailman/listinfo/ml)

Allora, premesso che ho dato un’occhiata veloce ecco che cosa ci vedo di  
strano… ma magari sono io che si possono fare ma che non avevo mai  
visto  
🙂

Non ho mai trovato prima d’ora la definizione di due :foreign\_key come  
hai  
fatto tu. Non è che se togli la seconda la vista non da più errori? Una  
prova la farei, giusto per togliermi il dubbio.

class Article \< ActiveRecord::Base  
belongs\_to :user, :class\_name =\> ‘User’, :foreign\_key =\> “author\_id”,  
:foreign\_key =\> “editor\_id” # elimina questa riga per prova, e la  
virgola  
sopra

Prova a fare così. A quanto segnala sembra che non riesca a trovare  
l’oggetto utente che è a nil e di conseguenza non riesce ad estrapolare  
nessun parametro, quindi basta capire perchè non lo becca ed il problema è  
risolto.

–Andrea R.
