Unknown key: :order

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 run this, I get the following error:

rake aborted!
ArgumentError: Unknown key: :order. Valid keys are: :class_name, ...
/home/john/src/ror/blog/app/models/user.rb:3:in `<class:User>'
/home/john/src/ror/blog/app/models/user.rb:1:in `<top (required)>'
/home/john/src/ror/blog/db/seeds.rb:9:in `<top (required)>'
Tasks: TOP => db:seed

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?

Any info would be greatly appreciated.

Thanks in advance,

John

Hi,

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 run this, I get the following error:

rake aborted!
ArgumentError: Unknown key: :order. Valid keys are: :class_name, ...
/home/john/src/ror/blog/app/models/user.rb:3:in `<class:User>'
/home/john/src/ror/blog/app/models/user.rb:1:in `<top (required)>'
/home/john/src/ror/blog/db/seeds.rb:9:in `<top (required)>'
Tasks: TOP => db:seed

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?

Any info would be greatly appreciated.

Thanks in advance,

John

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

This is really a rails question. Please direct your question over to the appropriate list/forum.

Thanks, I don't know how I missed that!

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:

== 20161114230702 CreateArticlesCategories: migrating

···

=========================
-- 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 11/14/2016 02:48 AM, zimbatm wrote:

Hi,

By googling: has_many rails order
I found this link which explains what to replace it whith: Deprecated warning for Rails 4 has_many with order - Stack Overflow

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 run this, I get the following error:

    rake aborted!
    ArgumentError: Unknown key: :order. Valid keys are: :class_name, ...
    /home/john/src/ror/blog/app/models/user.rb:3:in `<class:User>'
    /home/john/src/ror/blog/app/models/user.rb:1:in `<top (required)>'
    /home/john/src/ror/blog/db/seeds.rb:9:in `<top (required)>'
    Tasks: TOP => db:seed

    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?

    Any info would be greatly appreciated.

    Thanks in advance,