I have been learning Rails by working through Carneiro and Barazi's *Beginning Rails 3*. This has went well despite the fact that I am using Rails 4.2.6. I hit a little snag, that I think might be related to using a different version, but I have not been able to find out for sure through googling. The introductory project is a blog application, with users, profiles and articles. I am in chapter 5, which goes over associations. I hit a snag with trying to create a has_many association between a user and that user's articles, specifically in the code that is supposed to sort the user's articles first by order of creation, and then by alphabetical order. The big gives the following code for app/models/user.rb:
class User < ActiveRecord::Base
has_one :profile
has_many :articles, :order => 'published_at DESC, title ASC',
:dependent => :nullify
end
When I remove `:order => 'published_at DESC, title ASC',` from user.rb everything works fine. Am I missing something in the code, or is something different in Rails 4?
By googling: has_many rails order
I found this link which explains what to replace it whith:
···
On Mon, 14 Nov 2016, 04:35 John A, <welcome.to.eye.o.rama@gmail.com> wrote:
Hi all,
I have been learning Rails by working through Carneiro and Barazi's
*Beginning Rails 3*. This has went well despite the fact that I am using
Rails 4.2.6. I hit a little snag, that I think might be related to using
a different version, but I have not been able to find out for sure
through googling. The introductory project is a blog application, with
users, profiles and articles. I am in chapter 5, which goes over
associations. I hit a snag with trying to create a has_many association
between a user and that user's articles, specifically in the code that
is supposed to sort the user's articles first by order of creation, and
then by alphabetical order. The big gives the following code for
app/models/user.rb:
class User < ActiveRecord::Base
has_one :profile
has_many :articles, :order => 'published_at DESC, title ASC',
:dependent => :nullify
end
When I remove `:order => 'published_at DESC, title ASC',` from user.rb
everything works fine. Am I missing something in the code, or is
something different in Rails 4?
I have never encountered lambda scope blocks before, but I think the following change to app/models/user.rb is right:
class User < ActiveRecord::Base
has_one :profile
has_many :articles, -> { order 'published_at DESC, title ASC' } :dependent => :nullify
end
I went through the steps listed again, taking a stab that I could overwrite everything rather than just starting over. When I ran rake db:migrate, I got the following error:
=========================
-- create_table(:articles_categories, {:id=>false})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "articles_categories" already exists: CREATE TABLE "articles_categories" ("article_id" integer, "category_id" integer)
/home/john/src/ror/blog/db/migrate/20161114230702_create_articles_categories.rb:3:in `change'
ActiveRecord::StatementInvalid: SQLite3::SQLException: table "articles_categories" already exists: CREATE TABLE "articles_categories" ("article_id" integer, "category_id" integer)
/home/john/src/ror/blog/db/migrate/20161114230702_create_articles_categories.rb:3:in `change'
SQLite3::SQLException: table "articles_categories" already exists
/home/john/src/ror/blog/db/migrate/20161114230702_create_articles_categories.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
I did some poking around in the rails console, and running: category.articles.empty?
Returned: NameError: undefined local variable or method `category' for main:Object
Is there a way I can fix this without just deleting everything and starting over?
On Mon, 14 Nov 2016, 04:35 John A, <welcome.to.eye.o.rama@gmail.com > <mailto:welcome.to.eye.o.rama@gmail.com>> wrote:
Hi all,
I have been learning Rails by working through Carneiro and Barazi's
*Beginning Rails 3*. This has went well despite the fact that I am
using
Rails 4.2.6. I hit a little snag, that I think might be related to
using
a different version, but I have not been able to find out for sure
through googling. The introductory project is a blog application, with
users, profiles and articles. I am in chapter 5, which goes over
associations. I hit a snag with trying to create a has_many
association
between a user and that user's articles, specifically in the code that
is supposed to sort the user's articles first by order of
creation, and
then by alphabetical order. The big gives the following code for
app/models/user.rb:
class User < ActiveRecord::Base
has_one :profile
has_many :articles, :order => 'published_at DESC, title ASC',
:dependent => :nullify
end
When I remove `:order => 'published_at DESC, title ASC',` from user.rb
everything works fine. Am I missing something in the code, or is
something different in Rails 4?