Has_one :next - invalid name?


#1

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:inhas_one_without_reflection’
(eval):5:in `has_one’

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

Joe


#2

‘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.


#3

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.


#4

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


#5

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:inmodule_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:inhas_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:inload’
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:inrequire_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:inrequire_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:inconst_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.


#6

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)

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


#7

David,

bravo! there’s the reason.

Chris.