Rails:SQL 错误

发布于 2024-11-17 20:25:16 字数 928 浏览 5 评论 0原文

我收到一个 SQL 错误:

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

视图很简单:

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

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛上了 2024-11-24 20:25:16

看起来您想要 :order => 'comments.created_at desc',而不是sec

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

小苏打饼 2024-11-24 20:25:16

您收到的错误到底是什么?

我认为你的错误来自这里:

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

因为 comments.created at sec 你不能在数据库中拥有该字段

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文