Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Does rails console know the application?


  • Please log in to reply
10 replies to this topic

#1 fredrated

fredrated

    Signalman

  • Members
  • 11 posts

Posted 26 August 2013 - 06:14 PM

I am trying to use rails console in the course of trying to make my application work, but things don't seem to work in rails console like they do in my application.  I run rails console in a console in the application home directory.

 

In my employee controller, the following code executes and returns (mostly) what I expect:

@employee = Employee.find_by_number("10119")
@favorites = @employee.Favorite.find(:all)

Favorite is a join model and the second line executes just fine and returns what I expect.

However, in rails console:
@e = Employee.find_by_number("10119") works fine and returns the employee record.
But @f = @e.Favorite.find(:all) returns the error "undefined method 'Favorite' for #<Employee:....

What's up with this?  Why is the method found when executed in the controller, but not when executed in rails console?

Thanks.  -Fred



#2 Ohm

Ohm

    Guard

  • Members
  • 184 posts
  • LocationCopenhagen

Posted 26 August 2013 - 06:49 PM

Are you sure that the Employee model has a Favorite method? And what is this Favorite method suppose to return?


Blog: http://ohm.sh | Twitter: madsohm


#3 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 26 August 2013 - 07:00 PM

As explained in another thread

@favorites = @employee.Favorite.find(:all)

Is wrong

 

It should be just

@favorites = @employee.favourites

Programming is just about problem solving!


#4 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 26 August 2013 - 07:03 PM

Perhaps it would help if you read and digested this

 

http://guides.rubyon...ion_basics.html


Programming is just about problem solving!


#5 fredrated

fredrated

    Signalman

  • Members
  • 11 posts

Posted 26 August 2013 - 08:00 PM

Yes, James, you did mention that @favorites = @employee.Favorite.find(:all) is wrong, but in fact it works in my employee controller.

However, I will pragmatically defer to your suggestion, because the expression @favorites = @employee.favorites works in both the controller and the console, and I will not worry about why @employee.Favorite.find(:all) works in the controller and yet fails in the rails console.

Thanks for your help!



#6 Stuart Hannig

Stuart Hannig

    Signalman

  • Members
  • 15 posts

Posted 26 August 2013 - 08:00 PM

What's the reason to assign instance variables in console? Drop the @ symbol on your variable names.



#7 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 26 August 2013 - 08:07 PM

 

but in fact it works in my employee controller.

 

No it doesn't. It just looks like it does. Seriously you need to get your head round associations and how they work otherwise you will keep running into these problems.


  • Jamie likes this

Programming is just about problem solving!


#8 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 26 August 2013 - 08:08 PM

What's the reason to assign instance variables in console? Drop the @ symbol on your variable names.

 

None whatsoever :) Feel free to drop the @ symbol :)


Programming is just about problem solving!


#9 Jamie

Jamie

    Controller

  • Moderators
  • 114 posts
  • LocationThe UK

Posted 26 August 2013 - 08:33 PM

No it doesn't. It just looks like it does. Seriously you need to get your head round associations and how they work otherwise you will keep running into these problems.

 

James is correct and makes sense. I would follow the advice he has given and read the Rails Guides on associations :)


  • james and Jarrod like this
Rails developer based in Newcastle, UK.
Web app owner - Twitter lover

#10 fredrated

fredrated

    Signalman

  • Members
  • 11 posts

Posted 27 August 2013 - 04:10 PM

Thanks for all the advice.

1.  I have indeed, before posting here, read the Rails Guides on associations, and found them spectacularly lacking in useful information, which is why I ended up posting here.

2. As for  @employee.Favorite.find(:all), indeed it is slightly wrong, the upper case 'F' is wrong, but in fact  @favorites = @employee.favorite.find(:all) works perfectly well in the controller. When I use that expression in the employees controller, then place <%= @favorites.inspect %> in the view I get the list of favorites characteristics exactly as they exist in the database, so in fact the expression works in spite of the protestations to the contrary.

3. As for the use of the @ symbol in a variable in the console, does this affect the results of evaluation by the console interpreter?

4. I perfectly understand associations, having been a database developer and administrator for over 20 years.  It is rails that I can't get my head around.



#11 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 27 August 2013 - 05:14 PM

@favorites = @employee.favorite.find(:all)

 

 

I'm hoping that is just a type and that you meant

@favorites = @employee.favorites.find(:all)

notice the pluralisation of favourites It's important

 

@favorites = @employee.favorite.find(:all)

Might not be the most efficient way but may be useful if you are russian doll caching in your view but need to keep the data fresh otherwise you would normally just use

@favorites = @employee.favorites
@favorites = @employee.favorites.all

is the equivalent to

@favorites = @employee.favorite.find(:all)
3. As for the use of the @ symbol in a variable in the console, does this affect the results of evaluation by the console interpreter?

 

 

Makes no difference :) Drop it or use it whatever you prefer. I tend to use an instance variable just because it's what I'm used to doing. I'm actively working in three different development languages right now and I like to keep things as consistent as possible to help prevent my head exploding when switching between environments.
 

 

4. I perfectly understand associations, having been a database developer and administrator for over 20 years.  It is rails that I can't get my head around.

 

 

Rails is opinionated. If you follow the rules then it's a breeze. You just have to understand what those rules are.


  • Jamie likes this

Programming is just about problem solving!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users