0

I'm extremely new to RoR and am experiencing a roadblock in my learning. I created a scaffold against a performers db table that has a foreign key tie to a genres table as a lookup table for the performers genres.

What I'm attempting to do is to use the the genre_id from the current performer row in a find against the genres table to extract the genre name. I have everything working as desired in the list action except I can't seem to figure out how to add a "genre_name" => @genre[:genre_name] into the @performers instance variable so that it will propagate through to the view.

In my list action, I have the following code, but am obviously doing it wrong and would really appreciate if someone could steer me right.

  def list
    @performer_pages, @performers = paginate :performers, :per_page => 10
    @performers.each do |performer|
      #logger.debug(performer[:genre_id])
      @genre = Genre.find(performer[:genre_id], :select => 'genre_name')

      @performers[performer[:id]][:genre_name] = @genre[:genre_name]
      ^-- THIS IS THE LINE I AM STUCK ON
    end

    logger.debug(@performers.inspect)
  end

Thanks

1 Answer 1

1

You should be using associations, they will make this very easy.

In your Performer model, you should have a belongs_to :genre line.

In your Genre model, you should have a has_many :performers line.

Now in your code, you can access the name of a genre with this: (assuming you already have a @performer)

@performer.genre.genre_name

I hope this helps put you on the right track!

Sign up to request clarification or add additional context in comments.

6 Comments

Oh...I guess I should have mentioned that. In my performer model, I have has_one :genre and in my genre model I have belongs_to :performer. Should I change it to has_many?
Ahhh...so I got it backwards. No. In my database, I have a FK constraint whereas a performer has a genre_id field and genres is a lookup table for the genre_name based on that id. So, I need to invert the belongs_to and has_one then, yes?
Correct. Also Rails sometimes doesn't play well with FK constraints at the DB level, since it has its own rules for when and where it enforces them at the application level. Keep it for now, but just be aware of it.
I refrained from using cascading operations...just associative constraints for the purposes of having formal RDB structure. Thank you for that! Immensely grateful! So that part is fixed. Now about my original issue. How do I take the genre_name and add that to the @performers instance in that do loop or is that even necessary? I want it to be part of the loop used in the view to list everything.
You dont need to do that, in your view you can access it directly with the @performer.genre.genre_name in your display loop or whatever you might have. Also, so you avoid the N+1 problem of looking up each genre each time it is accessed you can add :include => :genre to the paginate line.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.