belongs_to and index methods

Subscribe to belongs_to and index methods 3 posts, 2 voices

 
Avatar cthornhill 2 posts

I have been working through the examples on the site about forms. I have reached one point that is giving me a lot of problems: As I went through the examples (airports and flights, etc…) I went ahead and added code to the index, edit, and show pages to display the new values appropriately. Checkboxes have some oddness (show and index are pretty different from edit and new, and locking does not appear to work right now…), but the origin and destination data has me stopped.

No matter what I try, even though my input pages work fine, I can not see a good way to get the airports.code (strings) to show in the flights index page. I have tested many options, but would really like some ideas on this, as it is a common pattern.

I am following the example code, and everything but this is fine. I just can’t see a way to get to the parent data column (airports.code – where the stings are) from my @flights data. I can build an @airports variable in the flight controller, but that does not do me lots of good since I can’t get to it from inside the simple loop that displays the flight data.

I am shure I am missing something easy, but I just can’t see it. .Net was not this frustrating, but I really want to start moving to Rails…

Thanks,

Cecil

PS – As a footnote, I am assuming that the ideal thing is to do something that can be implemented in the view to preserve the basic output of the index method. Since REST uses index and the other methods as an automatic Service Interface to a resouce, I guess making sure the numeric values from the foreign key tables is a good idea, though no person wants to know the destination of the flight is ‘3’ (they expect at least the string from the parent table). Clearly, one approach is to create a totally new display method for people, and leave index alone, but there is still the issue of how to best get the data in the most elegant way. I am guessing there is a simple way to do this that I am not seeing. BTW – normally I would expect to create or aggregate an object to do this in MS .Net, and in pure SQL of course you would write an SP to provide the join (or do it on the fly if just testing). I have not done ‘real’ joins in RUby/ActiveRecord and am interesting in how this all should be done. Thanks again for any help.

 
Avatar Jeff Cohen 89 posts

Hi Cecil,

Thanks for the question. I’m not sure I totally understand, though… can you post the code you’ve got in your index.html.erb (or index.rhtml)?

Regarding the PS: Good question. The “normal” thing to do in this case is to override the to_param method in your Flight class:

def to_param
  "#{self.number}"  # use the flight number, instead of raw id
end

URLs will now look like flights/A123, for example. Just beware that now, you have to translate differently when you load a flight based on the url:

def show
# Look up based on flight number
@flight = Flight.find_by_number(params[:id])
end

Jeff

 
Avatar cthornhill 2 posts

Jeff,

Thanks, that makes sense (re the PS). I will give it a try. I also got my basic question resolved – needed a brain re-boot. The issue was that I still had some nulls in my tables from setting the sample up. What I did to fix it/debug it was to run my code outside rails but using the dB.yml file and my models. I then looked at the loops and the results of the finds. I realized that the find for Airports (in this case @airports = Airports.find(:all)) in my controller was not passing me what I expected (at least I don’t think so…). I just wrote two little functions in the page that get the code for an origin or destination (yes I can go back and make one function I call twice…) and did a direct pull from the Airports class using the flight origin_id or destination_id (fo = Airport.find(flight.origin_id), and it all works fine.

I think lots of my issues came from dirty sample data and fuzzy thinking. The rest is just me getting used to what Rails and Ruby actually pass (is that really an array? could it be a proxy? may need to find out…).

Cecil