When using Audit in Rails 3, I get "uninitialized constant User::Audit" - ruby-on-rails-3

I was using acts_as_audited in my Rails 2 app and am in the process of updating it to Rails 3. I have updated a couple of models to call audited instead of acts_as_audited and those seem to work successfully. However, I am updating the User model of my app now and when I try to create a User (either from the console or from the app), I get the following error:
uninitialized constant User::Audit
The trace down the where it seems relevant is:
activerecord (3.2.8) lib/active_record/inheritance.rb:111:in `compute_type'
activerecord (3.2.8) lib/active_record/reflection.rb:172:in `klass'
activerecord (3.2.8) lib/active_record/associations/collection_association.rb:148:in `transaction'
activerecord (3.2.8) lib/active_record/associations/collection_association.rb:431:in `create_record'
activerecord (3.2.8) lib/active_record/associations/collection_association.rb:119:in `create'
activerecord (3.2.8) lib/active_record/associations/collection_proxy.rb:46:in `create'
audited (3.0.0) lib/audited/auditor.rb:216:in `block in write_audit'
activesupport (3.2.8) lib/active_support/callbacks.rb:403:in `_run__3460157709255055264__audit__3867610640799995319__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_audit_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
audited (3.0.0) lib/audited/auditor.rb:216:in `write_audit'
audited (3.0.0) lib/audited/auditor.rb:198:in `audit_create'
The line that creates the issue in my controller simply states:
if #editing_user.save
I've looked through the support list of audited's github account and can't find anything that fits my issue. I thought I'd ask here to see if someone has had a similar issue. I've also searched here and google to find an answer to this specific question but can't find anything relevant.
Any ideas?

It turns out that I had has_many :audits within the User model but not any other model. After I removed that has_many call, everything worked fine.

Related

Creating and authenticating user on post ("sign up"). Warden + Sinatra

TL;DR: How can I create a sign up feature with sinatra and warden?
I am trying to make a simple authentication system for a Sinatra app, and I found that warden is probably the best bet. I have found numerous examples on how to use it. I started working from the examples by sklise.
I quickly run into the problem of signing up. See that it is possible to create a new user with something like
post '/auth/signup' do
u = User.new(:username => params[:username], :password => params[:password])
u.save
But then what? How can I authenticate / sign this brand new user in? I cannot seem to find any single reference to how a sign up feature should be built with sinatra + warden. As a matter of fact, I can't seem to find anything for warden at all. None of the examples on the Warden wiki has a sign up feature. Do anyone have a solution for this?
Thanks
Not exactly a warden aficionado, but this looks like where you want to look:
https://github.com/hassox/warden/blob/906edf86c6c31be917a921097031b89361d022e8/lib/warden/strategies/base.rb#L116
From your example I think you'd want to do something like this:
post '/auth/signup' do
u = User.new(:username => params[:username], :password => params[:password])
u.save
env['warden'].success!(u)
...

Variant product_id with API Ruby gem

I'm using the Shopify_api gem to grab products and variants for an app. When grabbing a variant, I can't seem to get the product_id, even though the API Doc says it should be available. Anyone know how I can get the product_id from the variant?
http://docs.shopify.com/api/product_variant does say product_id should be in the response, and if you inspect the actual response it is. The problem is that ActiveResource doesn't seem to save this as a normal attribute, if you look at https://github.com/Shopify/shopify_api/blob/master/lib/shopify_api/resources/variant.rb, it uses this as a prefix option. The only way I figure out how to do it right now is
variant.prefix_options[:product_id]
This is also reported in ActiveResource here https://github.com/rails/rails/issues/3917

Difference between Active Model, Active Record and Active Resource

Is there anyone who can help me by defining the exact difference between Active Model, Active Record and Active Resource. I have done enough googling in order to find the exact difference, but didn't get anything concrete which can tell the exact difference between them. Right now they all look same to me. So please give me the appropriate answer with some concrete points.
Rails 3 is designed with modularity in mind. Each module has its own purpose and functionality.
ActiveModel: This component was created in Rails 3. They took all the model related parts that did not have a database requirement of Rails 2 ActiveRecord and moved it into ActiveModel. So ActiveModel includes things like validations. More information: http://www.rubyinside.com/rails-3-0s-activemodel-how-to-give-ruby-classes-some-activerecord-magic-2937.html
ActiveRecord: This is the component that associates a class to the database. This will give the class functionality such as methods that make it easy to pull records from the database (An example is the find method).
ActiveResource: Similar to ActiveRecord. However, instead of being backed by a database, an ActiveResource object is backed by another application through a web service API. More information: http://ofps.oreilly.com/titles/9780596521424/activeresource_id59243.html
(Couldn't figure out about ActiveBase... where did you hear it from?)
What I understand:
ActiveModel + Database Support = ActiveRecord
ActiveModel via WebService API = ActiveResource
ActiveModel https://github.com/rails/rails/tree/master/activemodel
Think of a super model who is in constant need of validation.
ActiveModel can be used for many things, but mostly recognized for adding validation support to models / db records.
ActiveRecord https://github.com/rails/rails/tree/master/activerecord
Think record as in table record.
Sets up a mapping between a new class and an existing table in a database.
In the context of an app, these classes are commonly referred to as models. Models can also be connected to other models; this is done by defining associations.
class Firm < ActiveRecord::Base
has_many :clients
has_one :account
belongs_to :conglomerate
end
In the background, rails uses ActiveRecord for schema management and defining properties for your records, acting as an ORM (object relational mapper):
"ORM: An object that wraps a row in a database table or view, encapsulates
the database access, and adds domain logic on that data."
A schema outlines the properties for a record.
ActiveResource https://github.com/rails/activeresource
Think resource like the R in URL or of the resource routing that powers many rails backends.
Allows you to do things like Create, Retrieve, Update, or Destroy (CRUD) via HTTP.
tyler = Person.find(1)
When a request is made to a resource route, a RESTful request maps itself its corresponding HTTP verbs and their database interactions
GET => Person.find(1)
POST => Person.new(:name => 'Tyler', :favorite_page => 'stackoverflow')
PUT => tyler.save
DELETE => tyler.destroy

binding.pry ignored by rails 3

I'm trying to debug my little app failing to create a Price because of an AssociationTypeMismatch. So I dig out pry, install, watch railscast and Josh's screencast and dig into the wiki.
I "cd" into the prices controller but that fails. I've put <% binding.pry %> into the new.html before the submit code but Rails says that the 'pry' method is unknown. pry is in my updated Gemfile and I restarted the server.
I put binding.pry into the create method of my prices_controller.rb and Rails happily breezes through it to throw the error I've already seen, not the pause, etc I was expecting.
Looking at the Price model, I'm not experienced enough to know what I should be looking for. What am I overlooking? thanx, sam
I have the exact same problem, my solution was to add pry-remote to my Gemfile, and call binding.remote_pry instead of binding.pry, open the terminal at your current project and execute pry-remote. happy debugging :)
It appears that in addition to the pry gem, I need the pry-rails gem as well. Now the binding.pry is recognized.
Instead of installing pry-rails you could just require 'pry' before the binding :-)

How pagination can be done in ruby on rails 3.1?

I heard that Pagination is included automatically for all controllers? How can it be implemented?
As far as I know, there's nothing included automatically. I believe there was built-in pagination in much earlier versions of Rails.
There are (at least) two gems that can be installed to do pagination. The will_paginate gem has been around for years and can be used in Rails 2 or 3. The more recent kaminari gem can be used in Rails 3.
The more recent kaminari gem can be used in Rails 3.
You can refer
https://github.com/amatsuda/kaminari
it is very simple to use.
Put this line in your Gemfile:
gem 'kaminari'
Then do bundle install
Typically, your controller code will look like this:
#users = User.order(:name).page params[:page]
On the views
Just call the paginate helper:
<%= paginate #users %>
Now you can see the paginated result..
As Don Roby said, there are several pagination gems and will_paginate and Kaminari are 2 popular ones. You can find some nice tutorial on pagination (incl. using these 2 gems) here : http://railscasts.com/episodes?utf8=%E2%9C%93&search=pagination