belongs_to + create_* = rails vs. console behavior difference = confused Pepe

Hi,

I have been battling something for a good hour and a half and finally
realized how to 'solve' the issue but I am very confused as of why I
should do what I just did.

I have 2 classes:

class User
  has_many :audits
  ...
end

class Audit
  belongs_to :user
  ...
end

The way things need to work is to first create an audit and after the
audit is created a user that has access to the audit gets created.
This has to work this way and not the other way around.

I have been testing my code in the console with the following:

audit = Audit.create(...)
audit.create_user(...)

In all instances 'audit' ended up being updated correctly with the
user ID.

However, when I executed a very similar code in my controller I got
different results.

My original control code:
  @audit = Audit.new(params[:audit])
  ...
  if @audit.save
    @audit.create_user(...)
    unless @audit.tenant_id
      # this code is never hit
    end
  end

Everything seems OK since the @audit.tenant_id seemed to have a value
but when the code was done running the database showed that the user
was created but the audit never got updated with the user ID. In order
to make it work I had to modify the code above as:

  @audit = Audit.new(params[:audit])
  ...
  if @audit.save
    @audit.create_user(...)
    @audit.save # <------- Why do I need to do this?
    unless @audit.tenant_id
      # this code is never hit
    end
  end

I am very confused because the console updates the audit record but
the controller doesn't.

??? Why ???

I'd appreciate an explanation of the difference in behavior.

Thank you.

I have 2 classes:

class User
has_many :audits
...
end

class Audit
belongs_to :user
...
end

The way things need to work is to first create an audit and after the
audit is created a user that has access to the audit gets created.
This has to work this way and not the other way around.

I have been testing my code in the console with the following:

audit = Audit.create(...)
audit.create_user(...)

In all instances 'audit' ended up being updated correctly with the
user ID.

However, when I executed a very similar code in my controller I got
different results.

My original control code:
@audit = Audit.new(params[:audit])
...
if @audit.save
@audit.create_user(...)
unless @audit.tenant_id

Why tenant_id and not user_id? I'm assuming that this is a typo or
you've changed your code to 'protect the innocent' and missed a
change.

I'll assume you meant user_id when you typed tenant_id.

 \# this code is never hit

end
end

Everything seems OK since the @audit.tenant_id seemed to have a value
but when the code was done running the database showed that the user
was created but the audit never got updated with the user ID. In order
to make it work I had to modify the code above as:

Okay, let's think about what those associations mean.

class User
    has_many :audits
    # means that we can find the audits for the user by finding all of
the rows in the audit table which have the key for this
    # User
    # There's no column for this association in the user table itself

class Audit
    belongs_to :user
    # This means that the audit table has a column, conventionally
called user_id which holds the key of the user which
    # 'has' this audit, or equivalently to which this audit belongs.

@audit = Audit.new(params[:audit])

   This creates a new Audit, which has the attribute values provided
by params[:audit]. Presumably this doesn't include a user_id value,
so the new instance of audit will have a nil value for user_id, so

...
if @audit.save

  This saves the newly created audit in the db, and since user_id is
nil then the column value for user_id will be null

@audit.create_user(...)

This creates a new user and saves it which assigns an id for the user,
it sets the user_id value in the instance of Audit, but DOES NOT SAVE
it.

@audit.save # <------- Why do I need to do this?
unless @audit.tenant_id
# this code is never hit
end
end

It's rare that you create a model with a has_many attribute from the
model which belongs_to it, it's normally done the other way around.
For one thing since a User has many audits, you don't normally create
a user each time you create an audit.

You would normally create and audit FOR a user rather than the other
way around either by:

    user = User.find(user_id) #where user id came from an action
parameter, or a form value

  then either

    audit.create(:user => user, ....)

or

    user.audits.create(audit_parameters)