# Has\_one :next - invalid name?

**URL:** <https://www.ruby-forum.com/t/has-one-next-invalid-name/76418>\
**Category:** Rails\
**Created:** [March 3, 2006, 10:03pm UTC](https://www.ruby-forum.com/t/has-one-next-invalid-name/76418 "2006-03-03T22:03:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [March 3, 2006, 10:03pm UTC](https://www.ruby-forum.com/t/has-one-next-invalid-name/76418/1 "2006-03-03T22:03:00Z")

</div>

I have this in a model:

has\_one :next,  
:class\_name=\>‘WorkPart’,  
:foreign\_key=\>‘next\_id’

And it causes this error:

compile error  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active\_record/deprecated\_associations.rb:83:  
void value expression

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active\_record/deprecated\_associations.rb:84:in  
`deprecated_has_association_method' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/associations.rb:472:in`has\_one\_without\_reflection’  
(eval):5:in `has\_one’

Is ‘next’ an invalid name? ‘previous’ works.

Joe

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [March 3, 2006, 11:28pm UTC](https://www.ruby-forum.com/t/has-one-next-invalid-name/76418/2 "2006-03-03T23:28:42Z")

</div>

‘next’ is a reserved word in Ruby. ‘previous’ is not a reserved word.  
that  
error is cryptic and does not really tell you much of what the cause  
could  
be. my advice is try something other than ‘next’ and if it goes away,  
problem solved.

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [March 4, 2006, 12:06am UTC](https://www.ruby-forum.com/t/has-one-next-invalid-name/76418/3 "2006-03-04T00:06:31Z")

</div>

It may be reserved in some situations, but I’ve used next and previous  
as instance methods on an AR model and had no trouble.

-Jonny.

Chris H. wrote:

> ‘next’ is a reserved word in Ruby. ‘previous’ is not a reserved word.  
> that  
> error is cryptic and does not really tell you much of what the cause  
> could  
> be. my advice is try something other than ‘next’ and if it goes away,  
> problem solved.

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [March 4, 2006, 5:26pm UTC](https://www.ruby-forum.com/t/has-one-next-invalid-name/76418/4 "2006-03-04T17:26:44Z")

</div>

Chris,

class Thing \< ActiveRecord::Base  
has\_one :next, :class\_name =\> ‘Thing’, :foreign\_key =\> ‘next\_id’  
end

‘next’ keyword belongs to Ruby language. Change the name of your  
association.

–  
Kent

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [March 4, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/has-one-next-invalid-name/76418/5 "2006-03-04T17:20:39Z")

</div>

I just did a quick test, and sure enough, I get the same thing.  
changing  
the association name to ‘next\_thing’ corrected the problem.

class Thing \< ActiveRecord::Base  
has\_one :next, :class\_name =\> ‘Thing’, :foreign\_key =\> ‘next\_id’  
end

> > t = Thing.new  
> > SyntaxError: compile error  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/deprecated\_associations.rb:83:  
> > void value expression  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/deprecated\_associations.rb:84:in  
> > `module_eval' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/deprecated_associations.rb:82:in`module\_eval’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/deprecated\_associations.rb:82:in  
> > `deprecated_has_association_method' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:472:in`has\_one\_without\_reflection’  
> > from (eval):5:in `has_one' from script/../config/../config/../app/models/thing.rb:2 from /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:207:in`load’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active\_support/dependencies.rb:207:in  
> > `load' from /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:39:in`require\_or\_load’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active\_support/dependencies.rb:22:in  
> > `depend_on' from /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:178:in`require\_dependency’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active\_support/dependencies.rb:178:in  
> > `require_dependency' from /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:194:in`const\_missing’  
> > from (irb):1

class Thing \< ActiveRecord::Base  
has\_one :next\_thing, :class\_name =\> ‘Thing’, :foreign\_key =\> ‘next\_id’  
end

> > t = Thing.new  
> > =\> #\<Thing:0x233bb7c @attributes={“next\_id”=\>nil, “name”=\>""},  
> > @new\_record=true\>

so my previous answer still stands, just change the name of the  
association  
and move on.

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [March 4, 2006, 5:32pm UTC](https://www.ruby-forum.com/t/has-one-next-invalid-name/76418/6 "2006-03-04T17:32:50Z")

</div>

Hi –

On Sat, 4 Mar 2006, Jonathan V. wrote:

> Chris H. wrote:
> 
> > ‘next’ is a reserved word in Ruby. ‘previous’ is not a reserved word.  
> > that  
> > error is cryptic and does not really tell you much of what the cause  
> > could  
> > be. my advice is try something other than ‘next’ and if it goes away,  
> > problem solved.
> 
> It may be reserved in some situations, but I’ve used next and previous  
> as instance methods on an AR model and had no trouble.

You may be able to define it explicitly, but when it’s an association  
name, AR tries to write a method like this, and runs into this error  
(shown here in irb):

> > def has\_next?; !next.nil?; end  
> > SyntaxError: compile error  
> > (irb):3: void value expression  
> > def has\_next?; !next.nil?; end  
> > ^

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [March 4, 2006, 5:38pm UTC](https://www.ruby-forum.com/t/has-one-next-invalid-name/76418/7 "2006-03-04T17:38:52Z")

</div>

David,

bravo! there’s the reason.

Chris.
