SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ruby on rails - help with an active record query

    Hi guys.

    I'm still at the early stages of learning so be gentle. However I used want to ask a question about an active record query I am trying to run.

    I have set up a table for my users and have populated in, created the model and I can queries against it. I now want the user to be able to visit http://localhost/profile/show/1 (1 being the user_id) and show the stories they have entered. This also has a model and is working properly as I have constructed an index page which lists recent articles and another page which shows them.

    However I can't get the articles for a selected user to appear on the profile page.

    My code is below...

    Code:
    class ProfileController < ApplicationController
    
      def show
      	@user = User.find(params[:id])
    	@stories = Story.find :all, :conditions => "user_id = params[:id]"	
      end
      
    end
    And...

    Code:
    <h1>Profile</h1>
    <p><%= @user.name %></p>
    
    <% for story in @stories %>
    	<%= story.headline %>
    <% end %>
    Thanks in advance.

  2. #2
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Akron, OH, USA
    Posts
    103
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Well, I don't know how your associations are set up but you should be able to do something like this in your show method:

    Code:
    @stories = user.stories
    And then use your view just like you have it.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply. I added @stories = @user.stories to my controller and it worked. However I think it needs to be part of a query as I want to order then my most recent.

    I've included my models below and I think I've set the relationship up correctly.

    Code:
    class Story < ActiveRecord::Base
    	belongs_to :user
    end
    Code:
    class User < ActiveRecord::Base
    	has_many :stories
    end

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The has_many options are listed in the Rails api:

    http://api.rubyonrails.org/classes/A...s.html#M001789

    You can do this:
    Code:
    class User < ActiveRecord::Base
    	has_many :stories, :order => 'created_at DESC'
    end
    However, that will return the stories in that order every time you use user.stories. If you only needed them ordered that way on some occasions you could create a custom method in the User model. Something like this for example:
    Code:
    class User < ActiveRecord::Base
      def stories_by_created_at
        Story.find(:all, 
              :conditions => ['user_id = ?', self],
              :order => 'created_at DESC')
      end
    end
    
    @stories = @user.stories_by_created_at

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Akron, OH, USA
    Posts
    103
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I posted a solution that was the same as Reggie's. His will work great!

  6. #6
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm pretty sure the problem is just that your variable isn't being interpreted. Replace the Story.find line with this:

    Code:
    @stories = Story.find :all, :conditions => "user_id = #{params[:id]}"
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys. Will try when I get back to the office on Monday morning and let you know how I get on.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much guys for your help. In the end this got it working.

    Code:
    @stories = Story.find :all, :conditions => "user_id = #{params[:id]}", :order => 'date_added DESC'

  9. #9
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd recommend you change:
    Code:
    @stories = Story.find :all, :conditions => "user_id = #{params[:id]}", :order => 'date_added DESC'
    to
    Code:
    @stories = Story.find(:all, :conditions => ["user_id = ?", params[:id]], :order => 'date_added DESC')
    The form you've used is susceptible to SQL injection attacks.

    Personally, I'd use:
    Code:
    if params[:id] and user = User.find(params[:id])
      @stories = Story.find(:all, conditions => ["user_id = ?", user], :order => 'date_added DESC')
    else
      #Some code that caught the fact that an invalid user id has been passed to the page
    end
    That way the user id is only used if a matching user can be found.

  10. #10
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the info.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •