The following code works in development like a charm, but in production no update is ever issued to the database. Basically I am trying to update a collection element that is nested 3 levels deep ..
def set_collection_value
#article = Article.find(params[:id])
#article.highlights.items.find(params[:item_id])
item.update_attributes(params[:values])
render :json => item
end
The model Article in question looks like this:
class Article
include Mongoid::Document
embeds_one :highlight, class_name: 'Highlight'
end
highlight.rb
class Highlight
includes Mongoid::Document
embedded_in :article
embeds_many :items, class_name: 'HighlightElement'
end
highlight_item.rb
class HighlightElement
include Mongoid::Document
embedded_in :highlight
field :title, type: String
field :teaser, type: String
field :image, type: String
field :body, type: String
attr_accessible :title, :teaser, :image, :body
end
The funny thing here is that even when I run webrick locally in production mode it works like a charm. Only once I deploy to my ubuntu server running mongodb v1.2.2 the above code silently does nothing.
I even went as far as to copy my environments/development.rb onto my environments/production.rb hoping to fix the issue.. but to no avail..
Any ideas? I am running rails 3.2.7 with mongoid 3.0.3
It would also be helpful if someone could point out how to make the moped/mongoid debug log messages show up in production.log. I configured Debugging as described in the logs - but these debug messages are only visible when running rails s - not in production.log
Solved it myself..
Turns out Mongodb 1.2.2 from the official Ubuntu repository sources was the problem.
After updating to 2.0.5 everything worked again correctly.
Related
I was trying to install gem FriendlyId. I am following railscast: 314-pretty-urls-with-friendlyid. It seems pretty easy to install. But not working for me.
Here are the steps I did:
added gem "friendly_id", "~> 4.0.9" in Gemfile
I then ran bundle install command
Then modified my product model to this:
class Product < ActiveRecord::Base
extend FriendlyId
friendly_id :name
attr_accessible :name, :product_code, :recipe, :selling_price, :servings, letsrate_rateable "quality", "packaging", "hygiene" , "service", "price"
validates :name, :presence => true
class << self
def search(params)
if params[:search]
products = Product.published.includes(:product_sub_categories)
end
end
end
end
Still it is showing 'id':
http://localhost:3000/products/9
As per railscasts it should show product name. But it is not. I individually install gem but no effect.
Can anybody tell what I am missing?
Not sure why this is not mentioned in Guide, but try:
alias_attribute :to_param, :slug
This works because to_param method allows to customise the id part of URL. And here you are assigning that part to friendly_id generated URL.
Another solution
I am not sure if this is implemented inside friendly_id, but I did this:
class Product < ActiveRecord::Base
def self.find(id_or_slug)
case id_or_slug
when String
find_by_slug id_or_slug
else
super id_or_slug
end
end
end
undefined method `selector' for #<ActiveRecord::Relation:0xbb8710c>
I am getting this error while trying to execute the follwoing code:
c = Content.first
c.is_processed = true
c.save # or c.update_attributes(:is_processed => true)
My Content model looks like this:
class Content
include Mongoid::Document
field :username
field :name
filed :is_processed, :type => Boolean
belongs_to :language
has_many :translations
end
I am using Mongoid 2.4, bson_ext 1.5, Ruby 1.9.2, Rails 3.2.5
I found the mistake. My Content model had this line:
has_many :translations
I had migrated the content model from ActiveRecord to Mongoid. But, all the other tables are still in ActiveRecord. The has_many was referring to a ActiveRecord model. This caused the issue. I commented the line and now update is working fine.
When I call paginate with kaminari on a collection of embedded documents I get the following error:
(Access to the collection for Document is not allowed since it is an embedded document, please access a collection from the root document.):
Any idea on how I can fix that ? I have installed kaminari as a gem.
Alex
You just need to access the collection through the parent object. For example, given the following models:
class User
include Mongoid::Document
embeds_many :bookmarks
end
class Bookmark
include Mongoid::Document
embedded_in :user
end
Then to paginate a given user's bookmarks you would do:
#user.bookmarks.page(params[:page])
I found this issue on Kaminari: https://github.com/amatsuda/kaminari/issues/89
So I forked it, and fixed it following the solution provided by spatrik. I am not 100% sure it will work on all cases and that this solution does not have any drawbacks. But for the moment it works exactly as expected.
Alex
I just sent a patch for the issue. Take a look at the request. hope this helps solve your issue.
https://github.com/amatsuda/kaminari/pull/155/files
With the previous theTRON example :
class User
include Mongoid::Document
embeds_many :bookmarks
end
class Bookmark
include Mongoid::Document
field :created_at, :type => DateTime
embedded_in :user
end
the following, will get you the error you described in your post :
#user.bookmarks.desc(:created_at).page(params[:page])
while the nex one will works fine :
#user.bookmarks.page(params[:page]).desc(:created_at)
I hope it help.
I'm fairly new to rails and TDD (as will no doubt be obvious from my post) and am having a hard time wrapping my brain around Rspec and FactoryGirl.
I'm using Rails 3, rspec and factory girl:
gem 'rails', '3.0.3'
# ...
gem 'rspec-rails', '~>2.4.0'
gem 'factory_girl_rails'
I have a user model that I've been successfully running tests on during development, but then needed to add an attribute to, called "source". It's for determining where the user record originally came from (local vs LDAP).
In my factories.rb file, I have several factories defined, that look something like the following:
# An alumnus account tied to LDAP
Factory.define :alumnus, :class => User do |f|
f.first_name "Mickey"
f.last_name "Mouse"
f.username "mickeymouse"
f.password "strongpassword"
f.source "directory"
end
I have a macro defined (that's been working up until now) that looks like this:
def login(user)
before(:each) do
sign_out :user
sign_in Factory.create(user)
end
end
I'm calling it in multiple specs like so (example from users_controller_spec.rb):
describe "for non-admins or managers" do
login(:alumnus)
it "should deny access" do
get :index
response.should redirect_to(destroy_user_session_path)
end
end
If I don't specify the "source" attribute, everything works OK, but as soon as I do, I get an error like so when running the test
12) UsersController for non-admins or managers should deny access
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `source=' for #<User:0x00000100e256c0>
I can access the attribute no problem from the rails console and the app itself, and it's listed in my attr_accessible in the user model. It's almost as though Rspec is seeing an old version of my model and not recognizing that I've added an attribute to it. But if I put the following line into my user model, the error disappears
attr_accessor :source
... which indicates to me that it is actually looking at the correct model.
Help!
How about running this?
rake db:test:load
[If you added a new attribute you'd need to migrate it to the test database.]
if you don't use schema.rb (e.g. you have set config.active_record.schema_format = :sql)
you should run
rake db:test:prepare
Setting up paperclip with S3 in my linux dev environment was a snap -- everything works out of the box. However, I can't get it to work on Heroku.
When I try to do an upload, the log shows:
Processing ItemsController#create (for 72.177.97.9 at 2010-08-26 16:35:14) [POST]
Parameters: {"commit"=>"Create", "authenticity_token"=>"0Hy3qvQBHE1gvFVaq32HMy2ZIopelV0BHbrSeHkO1Qw=", "item"=>{"photo"=>#<File:/home/slugs/270862_4aa601b_4b6f/mnt/tmp/RackMultipart20100826-6286-1256pvc-0>, "price"=>"342", "name"=>"a new item", "description"=>"a new item", "sold"=>"0"}}
Paperclip::PaperclipError (Item model missing required attr_accessor for 'photo_file_name'):
I found one blog post that referenced this error, and it said to add this to my model:
attr_accessor :photo_file_name
attr_accessor :photo_content_type
attr_accessor :photo_file_size
attr_accessor :photo_updated_at
That does indeed make the model missing required attr_accessor for 'photo_file_name' error go away, but it still doesn't work. See my other question for details. As I have figured out that with the attr_accessor lines added to my model the uploads fail even on my dev system, I suspect that is not the right answer.
Found the problem: needed to update the database.
heroku run rake:db:migrate
heroku restart
I had done what I thought would have accomplished the same thing already:
heroku rake db:schema:load
but perhaps that doesn't work or something went wrong in the process.
Error like this occurs if you create wrong column type in migration. When you define new table migration for paperclip, you need to specify t.attachment :name insted of t.string :name. Or add_attachment :table, :name when you add new paperclip column in existed table. And now you don't need to add these attributes in attr_accessor in model.
Well, this message seems to be because the columns it's missing. Try create a migration creating the columns:
class AddPhotoToEvent < ActiveRecord::Migration
def change
add_column :events, :photo_file_name, :string
add_column :events, :photo_content_type, :string
add_column :events, :photo_file_size, :integer
add_column :events, :photo_updated_at, :datetime
end
end
This work for me, here i have a table events with photo