RailsApps / rails-composer

Rails Composer. The Rails generator on steroids for starter apps.
http://www.railscomposer.com/
3.38k stars 486 forks source link

User model missing :created_at when using mongodb and haml #69

Closed pas256 closed 10 years ago

pas256 commented 11 years ago

I'm not sure what the exact cause is, but with the following combination, either the user model is missing the created_at field, or the view has an extra column that uses the created_at field in user.

Option 1 - remove extra field by deleting these lines from app/views/users/index.html.haml.

        %th Registered
...
          %td= user.created_at.to_date

Option 2 - add the field to app/models/user.rb

class User
  include Mongoid::Document
  rolify
  field :provider, type: String
  field :uid, type: String
  field :name, type: String
  field :email, type: String
  field :created_at, type: DateTime  # Add this line
  attr_accessible :role_ids, :as => :admin
  attr_accessible :provider, :uid, :name, :email
  # run 'rake db:mongoid:create_indexes' to create indexes
  index({ email: 1 }, { unique: true, background: true })

  def self.create_with_omniauth(auth)
    create! do |user|
      user.provider = auth['provider']
      user.uid = auth['uid']
      if auth['info']
         user.name = auth['info']['name'] || ""
         user.email = auth['info']['email'] || ""
      end
      user.created_at = DateTime.now  # Add this line
    end
  end

end

Hope that helps.

pas256 commented 11 years ago

Full option choices

    question  Install an example application?
          1)  I want to build my own application
          2)  membership/subscription/saas
          3)  rails-prelaunch-signup
          4)  rails3-bootstrap-devise-cancan
          5)  rails3-devise-rspec-cucumber
          6)  rails3-mongoid-devise
          7)  rails3-mongoid-omniauth
          8)  rails3-subdomains
   railsapps  Enter your selection: 1
      recipe  Running setup recipe...
       setup  Your operating system is darwin12.2.0.
       setup  You are using Ruby version 1.9.3.
       setup  You are using Rails version 3.2.11.
    question  Web server for development?
          1)  WEBrick (default)
          2)  Thin
          3)  Unicorn
          4)  Puma
       setup  Enter your selection: 3
    question  Web server for production?
          1)  Same as development
          2)  Thin
          3)  Unicorn
          4)  Puma
       setup  Enter your selection: 1
    question  Database used in development?
          1)  SQLite
          2)  PostgreSQL
          3)  MySQL
          4)  MongoDB
       setup  Enter your selection: 4
    question  How will you connect to MongoDB?
          1)  Mongoid
       setup  Enter your selection: 1
    question  Template engine?
          1)  ERB
          2)  Haml
          3)  Slim (experimental)
       setup  Enter your selection: 2
    question  Unit testing?
          1)  Test::Unit
          2)  RSpec
          3)  MiniTest
       setup  Enter your selection: 1
    question  Integration testing?
          1)  None
          2)  RSpec with Capybara
          3)  Cucumber with Capybara
          4)  Turnip with Capybara
          5)  MiniTest with Capybara
       setup  Enter your selection: 1
    question  Fixture replacement?
          1)  None
          2)  Factory Girl
          3)  Machinist
          4)  Fabrication
       setup  Enter your selection: 3
    question  Front-end framework?
          1)  None
          2)  Twitter Bootstrap
          3)  Zurb Foundation
          4)  Skeleton
          5)  Just normalize CSS for consistent styling
       setup  Enter your selection: 2
    question  Twitter Bootstrap version?
          1)  Twitter Bootstrap (Less)
          2)  Twitter Bootstrap (Sass)
       setup  Enter your selection: 2
    question  Add support for sending email?
          1)  None
          2)  Gmail
          3)  SMTP
          4)  SendGrid
          5)  Mandrill
       setup  Enter your selection: 1
    question  Authentication?
          1)  None
          2)  Devise
          3)  OmniAuth
       setup  Enter your selection: 3
    question  OmniAuth provider?
          1)  Facebook
          2)  Twitter
          3)  GitHub
          4)  LinkedIn
          5)  Google-Oauth-2
          6)  Tumblr
       setup  Enter your selection: 5
    question  Authorization?
          1)  None
          2)  CanCan with Rolify
       setup  Enter your selection: 2
    question  Use a form builder gem?
          1)  None
          2)  SimpleForm
       setup  Enter your selection: 1
    question  Install a starter app?
          1)  None
          2)  Home Page
          3)  Home Page, User Accounts
          4)  Home Page, User Accounts, Admin Dashboard
       setup  Enter your selection: 4
akshatpradhan commented 11 years ago

The solution is to put include Mongoid::Timestamps in the User Model. I just figured this out and now created_at works great.

That include should be part of rails composer in my opinion.

Reference. http://mongoid.org/en/mongoid/docs/extras.html#timestamps

Here's my full User Model now

class User
  include Mongoid::Document
  include Mongoid::Timestamps
  rolify
  field :provider, type: String
  field :uid, type: String
  field :name, type: String
  field :email, type: String
  has_many :entries
  attr_accessible :role_ids, :as => :admin
  attr_accessible :provider, :uid, :name, :email
  # run 'rake db:mongoid:create_indexes' to create indexes
  index({ email: 1 }, { unique: true, background: true })

  def self.create_with_omniauth(auth)
    create! do |user|
      user.provider = auth['provider']
      user.uid = auth['uid']
      if auth['info']
         user.name = auth['info']['name'] || ""
         user.email = auth['info']['email'] || ""
      end
    end
  end

end
DanielKehoe commented 10 years ago

MongoDB and Mongoid are no longer supported by Rails Composer, as reported in this blog post: MongoDB Dropped From Rails Composer.

However, if you're interested in restoring Mongoid as an option in Rails Composer, you can join the MongoDB Rescue Operation. See the blog post for details.

Thank you for your support.