Dan Rubio
Dan Rubio

Reputation: 4907

Rails Extensions syntax error: unexpected keyword_do_block

I've spent way too much time which should have otherwise been an easy fix. I have this code in my model.

class Forum < ActiveRecord::Base
  validates :name, presence: true
  belongs_to :project
  has_many :topics

15 has_many :posts, -> {order "posts.created_at DESC" }, through: :topics, do
    def last
      @last_post ||= find(:first, include: :account)
    end
  end
37 end

When I try to run this test below,

test "should list only top level topics" do
  assert_models_equal [topics(:sticky), topics(:il8n), 
                       topics(:ponies), topics(:pdi)],  
                       forums(:rails).topics
end

I get this annoying error

forum.rb:15: syntax error, unexpected keyword_do_block (SyntaxError)
forum.rb:37: syntax error, unexpected keyword_end, expecting end-of-input

I marked the lines above where the errors are happening. If I comment out the &extensions portion of the has many associations the test continues and errors out. What am I doing wrong? I've checked the Rails API syntax for the has_many associations shown below.

has_many(name, scope = nil, options = {}, &extension)

The syntax is in the correct order, so why is it returning a syntax error? Help would be appreciated. Thanks.

Upvotes: 1

Views: 384

Answers (1)

usha
usha

Reputation: 29349

you do not need a comma before do

has_many :posts, -> {order "posts.created_at DESC" }, { through: :topics } do

Have a look at this link

https://www.ruby-forum.com/topic/5436931

Upvotes: 1

Related Questions