Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    • CommentAuthorarthur
     
    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
     

    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
     

    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
     
    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
     

    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
     
    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.