Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Fix - Association Guarded Attributes #288

Conversation

Rican7
Copy link
Collaborator

@Rican7 Rican7 commented Apr 16, 2013

This fixes the association builder, so that you can build associated models without having to make the associated foreign_keys accessible to "mass assigning" (not something that you'd normally want to allow).

Example

<?php

class User extends Model
{
    static $has_many = array(
        array('emails'),
    );
}

/**
 * Email has three fields:
 * 'id', 'user_id', and 'email'
 */
class Email extends Model
{
    static $belongs_to = array(
        array('user'),
    );

    // Don't allow access to mass-set
    // our "id" or "user_id" attributes
    static $attr_accessible = array(
        'email',
    );
}

$user = User::find(1);
$user->create_email(array(
    'email' => 'example@example.com',
));
// FAILS

This Fails

THIS FAILS, with a foreign key exception, as the "create_email" method try's to set the foreign key of the model with "guard_attributes" set to true.

@Rican7 Rican7 closed this Apr 23, 2013
@Rican7 Rican7 reopened this Apr 24, 2013
@Rican7
Copy link
Collaborator Author

Rican7 commented Apr 24, 2013

Whoops. I accidentally closed this earlier. Re-opening...

@Rican7
Copy link
Collaborator Author

Rican7 commented Apr 24, 2013

I rebased off of the new master, so I could have Travis test with our new test-db, and all related tests passed.
It's still showing a fail, however, since there are other Travis bugs that currently exist (the find_by_datetime one #298).

I'd say this is safe to merge, but I'll wait for @al-the-x and @jpfuentes2 to weigh in.

@al-the-x
Copy link
Collaborator

Since this is related to the feature you've outlined in #287, maybe we should slate this for 1.2+ as well?

@Rican7
Copy link
Collaborator Author

Rican7 commented Apr 24, 2013

We could, but it affects anyone that uses $attr_protected and/or $attr_accessible, regardless of the security default in #287. This also doesn't break BC.

@al-the-x
Copy link
Collaborator

@Rican7, can you rebase on "master" and force-push to flatten the history before we merge this? I think this is probably the next safe candidate for merge.

Rican7 added 2 commits April 27, 2013 17:37
This is a non-API-breaking change. :)
correctly, while still setting the relationship keys correctly.
@Rican7
Copy link
Collaborator Author

Rican7 commented Apr 27, 2013

@al-the-x You want me to squash 2 commits that change 2 different files? Or just rebase off of master for the new testing?

I really should include tests in this fix, actually..

@Rican7
Copy link
Collaborator Author

Rican7 commented May 1, 2013

@al-the-x @jpfuentes2 any reason to not click the green button?

@al-the-x
Copy link
Collaborator

al-the-x commented May 1, 2013

Tests would be great. Squashes are appreciated. ;)

@Rican7
Copy link
Collaborator Author

Rican7 commented May 1, 2013

Ohhhhhh yea. I forgot that I said I was gonna write tests.
#mybad

(These tests fail without the associated fix)
@Rican7
Copy link
Collaborator Author

Rican7 commented May 3, 2013

Tests added. And they pass. :)
If you'd like to be extra thorough, try running the same tests without the fix in this PR, and they will fail.

In other words, this fixes the bug. :P

:shipit:

@Rican7
Copy link
Collaborator Author

Rican7 commented May 7, 2013

This delay, man. Its killing me. @al-the-x @jpfuentes2

@jpfuentes2
Copy link
Owner

I will hit the green button, but only because of:

If you'd like to be extra thorough, try running the same tests without the fix in this PR, and they will fail.
In other words, this fixes the bug. :P

💤

jpfuentes2 added a commit that referenced this pull request May 7, 2013
…d-attributes

Bug Fix - Association Guarded Attributes
@jpfuentes2 jpfuentes2 merged commit 0100b0d into jpfuentes2:master May 7, 2013
@Rican7
Copy link
Collaborator Author

Rican7 commented May 7, 2013

Haha. Thank you sir! :shipit:

@Rican7 Rican7 deleted the bug-fix-association-guarded-attributes branch May 7, 2013 21:14
@Rican7 Rican7 restored the bug-fix-association-guarded-attributes branch February 27, 2014 17:36
@Rican7 Rican7 deleted the bug-fix-association-guarded-attributes branch January 23, 2015 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants