0

I'm getting a SQL error with this:

Model:

class Comment < ActiveRecord::Base
    belongs_to :user
    belongs_to :entry

    attr_accessible :body

    validates :user_id, :presence => true
    validates :entry_id, :presence => true
    validates :body, :presence => true, :length => {:minimum => 10, :maximum => 5000}  #spam/stupid protection

    default_scope :order => 'comments.created at sec'
end

Controller

  def show
        @entry = Entry.find(params[:id])
            @comments = @entry.comments.all
  ...  
      respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @entry }
    end
  end

The view is a simple:

<% if @entry.state > 2 %>
    <section id="comments"> 
        <% @comments.each do |comment| %>
...loop some stuff...

2 Answers 2

2

It looks like you want :order => 'comments.created_at desc', not sec.

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

2 Comments

Yup. You got it. My bad here...I'm new to programming rails!
It's a learning process. :) In general, :order and :conditions, when given string arguments, interpret them as raw SQL, so keep that syntax in mind. If you need to do interpolation, you can use an array construct like so: ["foo = ?", bar] and bar will be sanitized.
0

And what exactly is the error you are receiving?

I think your error is from here:

default_scope :order => 'comments.created at sec'

Because comments.created at sec you can't have that field in DB

Comments

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.