SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question :limit Does not seem to work in SQL

    @user.stories_voted_on.find( :all, :limit => 6, :order => 'id DESC') is not returning proper number of records. There are 2 valid records but it returns 6.

    Code:
     
    def show
        @user = User.find_by_login(params[:id])
        @stories_submitted = @user.stories.find( :all, :limit => 6, :order => 'id DESC')
        @stories_voted_on = @user.stories_voted_on.find( :all, :limit => 6, :order => 'id DESC')
    end
    @stories_submitted seems to work, but @stories_voted_on do not.
    ---------------------------
    Conrad Parvin
    Web Development: http://www.parvin-associates.com
    Web Hosting: http://www.integrityhosting.biz

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How is stories_voted_on implemented?

  3. #3
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    How is stories_voted_on implemented?
    Code:
    class User < ActiveRecord::Base
      has_many :stories
      has_many :votes
      has_many :stories_voted_on, :through => :votes, :source => :story
    end
    It is implemented as a join on user votes and story tables.
    ---------------------------
    Conrad Parvin
    Web Development: http://www.parvin-associates.com
    Web Hosting: http://www.integrityhosting.biz

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What flavour of SQL are you using? Is it MS SQL and if so 2000 or 2005?

  5. #5
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does your Vote model have a belongs_to :story?

  6. #6
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ReggieB View Post
    What flavour of SQL are you using? Is it MS SQL and if so 2000 or 2005?
    I am using MySql as shipped with InstantRails
    ---------------------------
    Conrad Parvin
    Web Development: http://www.parvin-associates.com
    Web Hosting: http://www.integrityhosting.biz

  7. #7
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    Does your Vote model have a belongs_to :story?
    Code:
    class Vote < ActiveRecord::Base
    	belongs_to :story, :counter_cache => true
    	belongs_to :user
    end
    Yes, belongs_to :story is there

    (All code is cut/paste from my source code files)
    ---------------------------
    Conrad Parvin
    Web Development: http://www.parvin-associates.com
    Web Hosting: http://www.integrityhosting.biz

  8. #8
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's in your DB, and what's the schema?

  9. #9
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    What's in your DB, and what's the schema?
    Data:
    Code:
    Story Table
    id  	name  	 		link  					permalink  	user_id  	votes_count  	created_at  		description
    1  	My Shiny Weblog  	http://poocs.net  			my-shiny-weblog  	1  		0  	NULL  			NULL 
    2  	SitePoint Forums  	http://www.sitepoint.com/forums/  	sitepoint-forums  	1  		0  	NULL  			NULL 
    3  	Parvin Associates  	http://www.parvin-associates.com  	parvin-assocates  	1  		0  	NULL  			NULL 
    4  	WebMissions  		http://www.webmissions.org  		webmissions  		1  		0  	NULL  			NULL 
    5  	Parvin Family  		http://parvinfamily.com  		parvinfamily  		1  		0  	NULL  			NULL 
    6  	junk dot com  		http://www.junk.com  			junk-dot-com  		1  		1  	NULL  			NULL 
    8  	Joes Tavern  		http://youstabem.com  			joes-tavern  		1  		0  	NULL  			NULL 
    9  	just an other site  	http://anothersite.com  		just-another-site  	1  		0  	NULL  			NULL 
    10  	The Ruby Language  	http://www.ruby-lang.org  		the-ruby-language  	1  		1  	2007-09-02 13:41:03  	Ruby is an open source, object oriented scripting language invented by Yukihiro "Matz" Matsumoto in the early 1990s 
    11  	Where is Waldo  	http://www.whereiswaldo.com  		where-is-waldo  	1  		5  	2007-09-02 21:35:00  	Waldo is missing again. 
    
    User Table
    id  	login  		password  	name  		email
    1  	cparvin  	conradp  	Conrad Parvin  	conrad@parvin-associates.com 
    
    Votes Table
    id  	story_id  	created_at  	user_id
    1  	11  	2007-09-02 23:11:44  	1 
    2  	11  	2007-09-02 23:11:46  	1 
    3  	11  	2007-09-02 23:11:49  	1 
    4  	11  	2007-09-02 23:11:53  	1 
    5  	11  	2007-09-02 23:11:56  	1 
    6  	10  	NULL  			1
    Schema:
    Code:
    ActiveRecord::Schema.define(:version => 7) do
    
      create_table "stories", :force => true do |t|
        t.column "name",        :string
        t.column "link",        :string
        t.column "permalink",   :string
        t.column "user_id",     :integer
        t.column "votes_count", :integer,  :default => 0
        t.column "created_at",  :datetime
        t.column "description", :text
      end
    
      create_table "users", :force => true do |t|
        t.column "login",    :string
        t.column "password", :string
        t.column "name",     :string
        t.column "email",    :string
      end
    
      create_table "votes", :force => true do |t|
        t.column "story_id",   :integer
        t.column "created_at", :datetime
        t.column "user_id",    :integer
      end
    
    end
    ---------------------------
    Conrad Parvin
    Web Development: http://www.parvin-associates.com
    Web Hosting: http://www.integrityhosting.biz

  10. #10
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    class User < ActiveRecord::Base
      has_many :stories
      has_many :votes
      has_many :stories_voted_on, :through => :votes, :source => :story, :uniq => true
    end

  11. #11
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sylow View Post
    Code:
    class User < ActiveRecord::Base
      has_many :stories
      has_many :votes
      has_many :stories_voted_on, :through => :votes, :source => :story, :uniq => true
    end
    Added :uniq => true but this didn't change anything.
    ---------------------------
    Conrad Parvin
    Web Development: http://www.parvin-associates.com
    Web Hosting: http://www.integrityhosting.biz


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
  •