我正在尝试将我的项目升级到Rails 4,has_many ..:order =>已经改变了,所以我很容易重写简单的陈述,如:
has_many :trackbacks, :dependent => :destroy, :order => "created_at ASC"
到
has_many :trackbacks, -> { order 'created_at desc' }, :dependent => :destroy
然而,我一直试图重写:
has_many :comments, :dependent => :destroy, :order => "created_at ASC" do
# Get only ham or presumed_ham comments
def ham
where(:state => ["presumed_ham", "ham"])
end
# Get only spam or presumed_spam comments
def spam
where(:state => ["presumed_spam", "spam"])
end
end
我试过了:
has_many :comments -> { order 'position desc' do
# Get only ham or presumed_ham comments
def ham
where(:state => ["presumed_ham", "ham"])
end
# Get only spam or presumed_spam comments
def spam
where(:state => ["presumed_spam", "spam"])
end
end
}, :dependent => :destroy
但它不起作用..得到语法错误......可能是正确的语法
app/models/article.rb:53: syntax error, unexpected ->, expecting keyword_end
app/models/article.rb:63: syntax error, unexpected ',', expecting keyword_end
app/models/article.rb:96: syntax error, unexpected keyword_do_block, expecting =>
app/models/article.rb:97: syntax error, unexpected tSYMBEG, expecting keyword_end
app/models/article.rb:97: syntax error, unexpected ',', expecting keyword_end
app/models/article.rb:98: syntax error, unexpected '\n', expecting =>
app/models/article.rb:99: syntax error, unexpected '\n', expecting =>
app/models/article.rb:103: syntax error, unexpected '}', expecting =>
app/models/article.rb:107: syntax error, unexpected ',', expecting =>
app/models/article.rb:109: syntax error, unexpected '}', expecting =>
app/models/article.rb:110: syntax error, unexpected '}', expecting =>
app/models/article.rb:111: syntax error, unexpected '}', expecting =>
app/models/article.rb:291: syntax error, unexpected ')', expecting =>
app/models/article.rb:295: syntax error, unexpected ')', expecting =>
app/models/article.rb:531: syntax error, unexpected end-of-input, expecting keyword_end
app/controllers/application_controller.rb:42:in `get_latest_published_articles'
config/initializers/quiet_assets.rb:6:in `call_with_quiet_assets'
答案 0 :(得分:0)
你可以尝试一下:
has_many :comments, :dependent => :destroy, -> {where(:state => ["presumed_ham", "ham"]).order("created_at ASC")}