My First Rails App - Foreign Keys Questions

I’m in the middle of building my first rails app. Basically, it’s a
table of bands and artists which is related to (has many) a table of
tour dates.
I’ve got a few questions to see if I’m starting things right or if there
is a better way (there probably is).
I’ve got an admin controller which controls the artist table. You can
add/edit/all the usual scaffold stuff.
I’ve also got a tour_date controller which controls adding tour dates.
Here is my db schema (ignoring the parts that don’t matter much):

create_table “artists”, :force => true do |t|
t.column “artist_name”, :string, :default => “”, :null => false
t.column “active”, :boolean, :default => false, :null => false
t.column “bio”, :text, :default => “”, :null => false

end

create_table “tour_dates”, :force => true do |t|
t.column “artist_id”, :integer, :default => 0, :null => false
t.column “date”, :datetime, :null => false

end

add_index “tour_dates”, [“artist_id”], :name => “fk_artist”

In my ‘artist’ model I’ve got has_many :tour_dates
In my ‘tour_date’ model I’ve got belongs_to :artist
I’m not seeing a benefit of that yet, maybe someone could explain it.

I’ve set up a route:
map.connect ‘admin/tour/:id’, :controller => ‘tour_dates’, :action =>
‘list’, :id => nil

First, is this the best way to manage the tour dates from within the
‘admin’ area, or does my foreign key make anything more automatic?

Secondly, what’s the best way to show the artist_name in say a tour_date
view which only knows the artist_id?

And finally, in admin type views is it a bad idea to hard code things
like <%=h artist.artist_name %>? Seems OK to me, but it may not be the
Rails WAY.

Thanks, keep in mind, it’s my first try, I know these questions are
dumb. I really do love it so far.

in your controller:

class TourDatesController < Action…

def show
@artist = Artist.find(params[:id], :include => :tour_dates)
end
end

in your view:

@artist.artist_name
    <% @artist.tour_dates.each do |d| %>
  • <%= d.date%>
  • <%end%>
-------------------------
  1. you cant use local variables (“artist”) in views, only instance
    variables ("@artist") that have been initiated & filled previously in
    the controller action.
  2. the :include i used in the find method above includes all tourdates
    that belong to the artist who has id = :id into the find result.

so one SQL query gets you the artist info like artist_name and the tour
dates.

dont know what you mean by “hardcoding” stuff in the view …

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs