Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Dynamic method calling (send)


  • Please log in to reply
4 replies to this topic

#1 wazon

wazon

    Passenger

  • Members
  • 5 posts

Posted 12 November 2013 - 07:12 AM

Hello everyone,

this is mainly a ruby problem I'm having in my rails app. I have 2 models, let's say "Question" and "Answer". Every Question has one Answer and every Answer belongs to Question. In my controller, I'm trying to achieve this:

@question = Question.new
@question.answer = Answer.new
but using "answer" from a parameter:
 
@question.send(params[:answer].downcase) = send(params[:answer]).new
 
but I'm getting:
 
SyntaxError: (irb):87: syntax error, unexpected '=', expecting $end
 
 
If I just do:
 
@question.send(params[:answer].downcase)
=> {}
 
it works perfectly. Does anyone know why is this happening?
 
Regards,
Wazon


#2 Jamie

Jamie

    Controller

  • Moderators
  • 114 posts
  • LocationThe UK

Posted 12 November 2013 - 08:32 AM   Best Answer

Are your relationships setup correctly and is there anything more to your page or the models? Please post both here as it looks like you have one to many 'end''s.

 

I'd use the build method in this case and do the following and let me know if it gives you an error..

@question.answer.build

Or if a question can has_many Answers then use

@question.answers.build

Hope this helps :)


  • wazon likes this
Rails developer based in Newcastle, UK.
Web app owner - Twitter lover

#3 wazon

wazon

    Passenger

  • Members
  • 5 posts

Posted 12 November 2013 - 09:04 AM

Thank you! It works perfectly with little changing :)

@question.method("build_#{params[:answer].downcase}").call

maybe it requires some "prettyfying", but at least it works :)

 

When I try to execute your exact line I get this:

@question.answer.build

NoMethodError: undefined method `build' for nil:NilClass
        from (irb):3
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'

As you have requested, my models:

 

class Question < ActiveRecord::Base
  has_one :answer

  accepts_nested_attributes_for :answer

  attr_accessible :text, :answers_attributes
end
 
class Answer < ActiveRecord::Base
  belongs_to :question

  attr_accessible :text
end
 
 
Tank you again and regards,
Wazon


#4 Jamie

Jamie

    Controller

  • Moderators
  • 114 posts
  • LocationThe UK

Posted 12 November 2013 - 10:53 AM

I assume looking at your code you're not using Rails 4? What Rails -v and Ruby -v are you using?


Rails developer based in Newcastle, UK.
Web app owner - Twitter lover

#5 wazon

wazon

    Passenger

  • Members
  • 5 posts

Posted 12 November 2013 - 11:07 AM

# ruby -v
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-linux]
 
# rails -v
Rails 3.2.13
 





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users