Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Just started with ActiveRecords association, little confused


  • Please log in to reply
1 reply to this topic

#1 bizt

bizt

    Passenger

  • Members
  • 4 posts

Posted 27 January 2014 - 02:26 PM

OK, I'm just getting to grips with ActiveRecords and associations. The app I'm practising with is a money management tool. It allows users to add accounts and transactions with the intention of monitoring their finances.

 

So I've managed to provide user login with Devise. I've just put together my Account model and controller. In the models I have done the followiing:

class User < ActiveRecord::Base
  has_many :accounts, dependent: :destroy
end
 
class Account < ActiveRecord::Base
  belongs_to :user
end

In my account controller I've done this:

def new
    @account = current_user.accounts.build()
end

When I debug @accout in my view I can see that user_id is present. Great!

 

In my views/account/new.html.erb I have the following for the form:

<%= form_for @account, url: {action: "create"} do |f| %>
  <div><%= f.label :name %><br />
  <%= f.text_field :name, :autofocus => true %></div>
  
  <%= f.submit "Create" %>
<% end %>

OK, up until now I have some questions...

 

Do I need to put every account attribute in the form? I don't want to have to put user_id as I don't want that to be handled from the form if it's not needed to be. Also, I have another attribute called "amount". I don't really want this on the form as it will be created with a default value of 0. Do I handle this as a hidden form element, or in the controller when it handles the submission?

 

How do I handle the submission in the account_controller create method? Like so:

def create
    @account = Account.new(account_params) # so I do need to pass user_id..?
    if @account.save
      flash[:success] = "Account created"
      redirect_to @account
    else
      render 'new'
    end
end

Sorry if these are questions that are answered in many tutorials, I found tutorials handling form submission but what to do with the association kinda confuses me a little. I just want to not get into any wrong habits at this early stage and try to do things the "rails way". Any help would be much appreciated. Thanks



#2 bizt

bizt

    Passenger

  • Members
  • 4 posts

Posted 28 January 2014 - 02:39 AM

Actually, after a sleep and a second look I figure it should be like this for the form submission:

def create
  account_params[:amount] = 0 # perhaps i should set this default value here?
  @account = current_user.accounts.new(account_params) # used current_user.accounts.build instead to populate user_id
    if @account.save
      flash[:success] = "Account created"
      redirect_to @account
    else
      render 'new'
    end
end

Sorry, I've got my files at home so can't test this out now to see if it works or not. This seems to answer my question though if it does. I would like to find a way to set a default value for amount though. Perhaps I should do that in the model (before_save?), or the table (using a migration?). I can investigate this myself anyway.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users