Reputation: 63
Hi Rails Beginner here,
I followed a tutorial guide that helped auto generate a bunch of files to help with the CRUD process of entities in my models, for my entities (books), they have a title, number of total copies, and number of available copies. Since new books should always have the total number of copies available, I want to remove the redudancy of asking the user to input the number of available copies by making the form field that takes in total number of copies be saved into 2 variables (total and available). But I'm not sure how to do it.
Upvotes: 0
Views: 44
Reputation: 2715
It's always best to attach a code example to your questions.
Here are two ways how I would do it:
# controller.rb
def create
@book = Book.new(book_params)
@book.total_copies = params.require(:book)[:available_copies]
if @book.save
...
end
Beware that the code is just a guess, you need to adapt it and use the column names from your DB
Upvotes: 1