Not signed in (Sign In)
Welcome Guest!
Want to take part in these discussions? If you have an account, sign in now.
If you don't have an account, apply for one now.
    • CommentAuthorarthur
    • CommentTimeNov 16th 2007
     
    Hello!

    I'm having a hard time understanding has_many and the scaffold. I've got an Album that contains many Tracks (var $has_many = 'tracks') and a track belongs to an album. However, in a scaffold generated edit action a $this->album magically appears from nowhere. I cannot see anywhere where it gets instantiated. And from what i can read, i need to add array('include' => 'track') but as there are no $this->album = $Album->find(id)... so there is not anywhere i can add the array() argument.

    How do i load the $this->album->tracks field? The edit-action does not generate an inner join. What do i need to do to fill the tracks field?
    Also, where does $this->album instantiated?

    Regards.
    • CommentAuthorKaste
    • CommentTimeNov 17th 2007
     

    Controllers instantiate the given or supposed/guessed model automatically. see lib/AkActionController; method: ->instantiateModelClass

    With a given id in the url they do a find(id).

    Simply add $this->album = [...] to your edit-action.

    •  
      CommentAuthorbermi
    • CommentTimeNov 17th 2007
     

    Arthur,

    $this->album->track->load();
    

    will load tracks on

    $this->album->tracks
    

    AkActionController::instantitiateModelClass instantiates models in controllers and makes them available under $this->.

    If params['id'] is available, it will load the model named after the controller using that id.

    Until now, it was not possible to define the options for the automatic finder in a simple way. I've just added the ability to define which options should be passed to the finder method by using the finder_options attribute like

    class AlbumController extends ApplicationController{
        var $finder_options = array('Album' => array('include' => 'tracks')); // Album case matters
        // ....
    }
    

    Note: this only works on the trunk version.

    • CommentAuthorarthur
    • CommentTimeNov 18th 2007
     
    That's great! It felt kind of redundant having to add array('include' => 'tracks') at every find() function call. But as the model has the $has_many variable, doesn't the find() function automatically know that it should perform a join?
    •  
      CommentAuthorbermi
    • CommentTimeNov 18th 2007
     

    Imagine you have a User model which is related to posts, videos, images, addresses, credit cards, invoices and so on. That is a common scenario and it is unlikely you want to instantiate all the associated models every time you load a user.

    • CommentAuthorarthur
    • CommentTimeNov 19th 2007
     
    That's true! But doesn't rails (if one should compare..) work as it includes all associations unless you specify that it shouldn't? In contrast to not including any associations unless you specify that it should.
Add your comments
    Username Password
  • Format comments as