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 when in chapter 5, which goes over associations, I tried 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. I fixed this problem, with a little help from the Ruby Talk mailing list, with a lambda scope block, as follows in app/models/user.rb:
class User <
ActiveRecord::Base
has_one
:profile
has_many :articles, → { order ‘published_at DESC, title
ASC’ } :dependent => :nullify
end
This seemed to work, so 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:
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 when in chapter 5, which goes over
associations, I tried 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.
I fixed this problem, with a little help from the Ruby Talk mailing list,
with a lambda scope block, as follows in app/models/user.rb:
class User < ActiveRecord::Base
has_one :profile
has_many :articles, -> { order 'published_at DESC, title ASC' }
:dependent => :nullify
end
This seemed to work, so 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:
You are trying to run a migration
(20161114230702_create_articles_categories.rb)
that adds that table, but the table already exists in the database. Have
you added a second migration that tries to add it again? If it is not that
then the easiest solution may be to delete and recreate the db again.