activemodel

Rails 3, RSpec 2.5: Using should_receive or stub_chain with named scopes

隐身守侯 提交于 2019-12-31 10:32:21
问题 I use Rails 3.0.4 and RSpec 2.5. In my controllers I use named scopes heavily, for example @collection = GuestbookEntry.nonreplies.bydate.inclusive.paginate( :page => params[:page], :conditions => { ... }) In my tests, I want to be able to mock the result of such a query, not the wording . I do not think it makes sense to do something like GuestbookEntry.stub_chain(:nonreplies, :bydate, ...).and_return(...) because this test will fail the moment I decide to reorder the named scopes. With

How to determine if a record is just created or updated in after_save

纵然是瞬间 提交于 2019-12-29 11:31:33
问题 The #new_record? function determines if a record has been saved. But it is always false in the after_save hook. Is there a way to determine whether the record is a newly created record or an old one from update? I'm hoping not to use another callback such as before_create to set a flag in the model or require another query into the db. Any advice is appreciated. Edit: Need to determine it in after_save hook, and for my particular use case, there is no updated_at or updated_on timestamp 回答1: I

Rails - Help understanding how to use :dependent => :destroy

点点圈 提交于 2019-12-29 09:09:10
问题 I have the following models: User (id) Project (id) Permission (project_id, user_id) Thread (project_id) ThreadParticipation (thread_id, user_id) So that's working nicely, problem is this. When a user leaves or is removed from a project, I need all their ThreadParticipation's for that project deleted. Example, so if user(15), leaves project(3) by deleting the permission (user_id =>15, project_id => 3), I need rails to automatically then delete all related ThreadParticipation records (where

Format xml with properties with acts_as_api

和自甴很熟 提交于 2019-12-25 05:05:44
问题 i have a doubt with acts_as_api. i want to format a xml string like this: <root> <child property="property_value">Some Text</child> etc </root> this is possible? thank's in advance 回答1: I think this will not work automagically as you want to put data in your XML structure in two different places: The properties of the nodes The content of the nodes But the attributes of your model are contained a single Hash. acts_as_api (or another serializer) can't determine which attributes are supposed to

Active Model XML serialization : undefined method `[]' for nil:NilClass

拥有回忆 提交于 2019-12-24 14:34:52
问题 Originally from here, i'm posting a new thread to focus on my issue resolving around the "to_xml" method. What i'm trying to do : make a conversion from a ruby Object (Whois::Record) to XML. I am currently using ActiveModel but not ActiveRecord (eg previous thread for reasons) Here is the relevant controller : def lookup domain = params[:domain] @whois = Request.new @whois.search(domain) respond_to do |format| if @whois != nil format.html format.json {render :json => @whois.body} format.xml

Mass-assign exception no explanation found (Rails 3.2.1)

依然范特西╮ 提交于 2019-12-23 17:00:39
问题 Please see this post as well. Note: The current rake task saves User and Topic objects, but not posts or tags when setting a post object. Question: What is a proper way to describe this relationship in my rake task (shown below) so that it does not throw a can't mass assign attributes exception? Here were my steps: Step 1: Create models class Post < ActiveRecord::Base belongs_to :topic, inverse_of: :posts has_and_belongs_to_many :tags attr_accessible :title, :description, :content, :tags

rails 3.2.0 and heroku

做~自己de王妃 提交于 2019-12-23 07:57:57
问题 Hey Guys I'm having some troubles with rails 3.2.0 & ruby 1.9.3, I only created a new app with rails new command and I'm trying to deploy to heroku. I'm having the following app error: An error occurred in the application and your page could not be served. Please try again in a few moments. If you are the application owner, check your logs for details. After run heroku logs I get the following error: Could not find activemodel-3.2.0 in any of the sources Thanks in advance!!!! GemFile source

Rails 3 ActiveModel: cannot include ActiveModel::Model directly

旧时模样 提交于 2019-12-22 05:11:52
问题 In my Rails 3.2.11 and "development" environment when I try to have an active model: class DisponibilityApi include ActiveModel::Model attr_accessor :start_time, :end_time validates :start_time, :end_time, :presence => true end I have an error: NameError: uninitialized constant ActiveModel::Model But when I include it manually: class DisponibilityApi extend ActiveModel::Naming extend ActiveModel::Translation include ActiveModel::Validations include ActiveModel::Conversion attr_accessor :start

Track dirty for not-persisted attribute in an ActiveRecord object in rails

邮差的信 提交于 2019-12-20 16:30:48
问题 I have an object that inherits from ActiveRecord, yet it has an attribute that is not persisted in the DB, like: class Foo < ActiveRecord::Base attr_accessor :bar end I would like to be able to track changes to 'bar', with methods like 'bar_changed?', as provided by ActiveModel Dirty. The problem is that when I try to implement Dirty on this object, as described in the docs, I'm getting an error as both ActiveRecord and ActiveModel have defined define_attribute_methods , but with different

Track dirty for not-persisted attribute in an ActiveRecord object in rails

纵饮孤独 提交于 2019-12-20 16:30:31
问题 I have an object that inherits from ActiveRecord, yet it has an attribute that is not persisted in the DB, like: class Foo < ActiveRecord::Base attr_accessor :bar end I would like to be able to track changes to 'bar', with methods like 'bar_changed?', as provided by ActiveModel Dirty. The problem is that when I try to implement Dirty on this object, as described in the docs, I'm getting an error as both ActiveRecord and ActiveModel have defined define_attribute_methods , but with different