0

I use rails g scaffold to create a new group of controller, model and view, which allows users to input a int number. They can only edit the only number but not create a new one or delete it.

rails g scaffold number numa:integer

I would like to monitor what users input, and compare the number and call a function in another controller which I have already created before. However, I am quite new to rails on ruby, and do not have any idea about how to do that?

What I am pretty sure is that what users input will be stored in the a table which is bind with the new scaffold. The table's name is 'numbers', and the only inputbox for the only number's name is "numa". What should I do in another controller (lets call it "foods_controller.rb") to access the number. Maybe numbers.numa ?

user2049259
  • 533
  • 2
  • 6
  • 13

1 Answers1

1

First off, I recommend popping through the Ruby on Rails guide to ActiveRecord

To specifically answer your question, a couple of different options:

You can monitor the table contents through your model. (More preferable, probably)

Try opening up a rails console (rails console, rails c, or irb), and typing in Number.all, which will return a list of all Number records stored in the table. There are many other queries which can accomplished this way; check out the ActiveRecord::Base API doc for more info.

OR

You can perform database queries directly on the database table you created.

EDIT: Since you want to perform this in another controller, use the first method, calling Number.all to get a list of all Number records stored in your database. You can also call Number.first, Number.last or Number.find(<number_id>) if you only want a single record to be returned.

gdpelican
  • 568
  • 4
  • 12
  • Thank you very much for you help. Your answer is quite useful for me. However, I cannot find a proper way to select one of variables in table. Here is the structure of my table which is generated by rails c and Number.all: Number Load (0.2ms) SELECT "numbers".* FROM "numbers" => [# "numa") but failed.. Thank you for your help! – user2049259 Apr 27 '14 at 01:51
  • Once you have the record, you can select individual fields from it like so: `Number.first.numa` Or, you could use 'pluck', which selects a single field from your table. `Number.pluck(:numa).first` – gdpelican Apr 27 '14 at 03:01