Forum: Ruby on Rails has_one :next - invalid name?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Joe (Guest)
on 2006-03-03 23:03
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
Chris H. (Guest)
on 2006-03-04 00:28
(Received via mailing list)
'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.
Jonathan V. (Guest)
on 2006-03-04 01:06
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.
Chris H. (Guest)
on 2006-03-04 18:20
(Received via mailing list)
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.
Kent S. (Guest)
on 2006-03-04 18:26
(Received via mailing list)
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
unknown (Guest)
on 2006-03-04 18:32
(Received via mailing list)
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
Chris H. (Guest)
on 2006-03-04 18:38
(Received via mailing list)
David,

bravo!  there's the reason.

Chris.
This topic is locked and can not be replied to.