Transaction is a reserved class in Rails.
-----
Ryan Bigg
Freelancer
http://frozenplague.net







On 14/12/2008, at 9:13 PM, Greg Hauptmann wrote:

> still stuck here
>
> When I create a new "allocation" model object, I check it is valid  
> OK, but when I "save!" it I just get a "nil"?  What would this  
> imply.  There's no error as such.  It is true to say that I  
> populated the non-null columns with relationship with ID's of just  
> "1" (i.e. didn't ensure there was actually a matching record in  
> their tables).  Also the DB doesn't have foreign key constraints for  
> these relationships.   Questions here:
>
> Q1 - Does rails check to see that there is a valid object in an  
> association present before allowing the save?  (i.e. via the fact  
> that the model has a "belongs_to" in it?
>
> Q2 - If it does do this check what would be the expected output from  
> Rails the object wasn't there in the associated table (e.g. if one  
> put manually a bad reference ID in)?  Would it be "nil" as I got?   
> There wouldn't be a more specific exception raised?  especially if  
> one is using the "save!" method?
>
>
> *** CONSOLE OUTPUT ***
> >> a = Allocation.new
> => #<Allocation id: nil, transaction_id: nil, person_id: nil,  
> recurring_id: nil, amount: nil, amount_percent: nil, created_at:  
> nil, updated_at: nil>
> >>
> ?> a.valid?
> => false
> >> a.amount = 1
> => 1
> >> a.transaction_id = 1
> => 1
> >> a.person_id = 1
> => 1
> >>
> ?> a.valid?
> => true
> >>
> ?>
> ?> a.save
> => nil
> >> a.save!
> => nil
>
> ** SQL FROM ./SCRIPT/SERVER WHEN I DID THE "a.save!" ***
>   Transaction Columns (0.003291)   SHOW FIELDS FROM `transactions`
>   Transaction Load (0.001494)   SELECT * FROM `transactions` WHERE  
> (`transactions`.`id` = 1)
>
> ** Model code **
> #
>
> class Allocation < ActiveRecord::Base
>   belongs_to :person
>   belongs_to :transaction
>
>   validates_numericality_of :amount, :if => :amount
>   validates_numericality_of :amount_percent, :if => :amount_percent
>
>   private
>
>   def validate
>     errors.add_to_base('amount and amount_percent can not both be  
> specified') if amount && amount_percent
>     errors.add_to_base('either amount OR amount_percent must be  
> specified') if !amount && !amount_percent
>   end
>
> end
>
>
>
>
> On Fri, Dec 12, 2008 at 5:09 PM, Greg Hauptmann 
> <[email protected] 
> > wrote:
> Hi,
>
> I have a model for which when I go to save an item it doesn't seem  
> to get saved.  In the console I don't get a "record not saved"  
> error???  But rather the response seems to give me back a  
> Transaction object (i.e. for which the saved Allocation object has a  
> relationship with)?  Any ideas why?
>
> CONSOLE OUTPUT
> ?> a = Allocation.new
> => #<Allocation id: nil, transaction_id: nil, person_id: nil,  
> recurring_id: nil, amount: nil, amount_percent: nil, created_at:  
> nil, updated_at: nil>
> >> a.valid?
> => false
> >> a.transaction_id = 1784
> => 1784
> >> a.person_id = 1
> => 1
> >> a.amount = 100
> => 100
> >> a.valid?
> => true
> >> a.save!
> => #<Transaction id: 1784, transaction_date: "2009-02-04",  
> bank_account_id: 5, category_id: 6, recurring_id: 3, amount:  
> #<BigDecimal:22291e0,'0.0',4(8)>, balance: #<BigDecimal: 
> 2229190,'0.1E4',4(12)>, description: "food", notes: nil, created_at:  
> "2008-12-08 21:21:17", updated_at: "2008-12-08 21:21:17",  
> projection: true>
> >> a
> => #<Allocation id: nil, transaction_id: 1784, person_id: 1,  
> recurring_id: nil, amount: #<BigDecimal:2218160,'0.1E3',4(8)>,  
> amount_percent: nil, created_at: nil, updated_at: nil>
> >>
>
> MODEL
> -----------------------------------------------------------------------------------------------------------------------
> Macintosh-2:myequity greg$ cat app/models/allocation.rb
> # == Schema Information
> # Schema version: 20081128104846
> #
> # Table name: allocations
> #
> #  id             :integer(4)      not null, primary key
> #  transaction_id :integer(4)      not null
> #  person_id      :integer(4)      not null
> #  recurring_id   :integer(4)
> #  amount         :decimal(9, 2)
> #  amount_percent :decimal(9, 2)
> #  created_at     :datetime
> #  updated_at     :datetime
> #
>
> class Allocation < ActiveRecord::Base
>  belongs_to :person
>  belongs_to :transaction
>
>  validates_numericality_of :amount, :if => :amount
>  validates_numericality_of :amount_percent, :if => :amount_percent
>
>  private
>
>  def validate
>    errors.add_to_base('amount and amount_percent can not both be  
> specified') if amount && amount_percent
>    errors.add_to_base('either amount OR amount_percent must be  
> specified') if !amount && !amount_percent
>  end
>
> end
> -----------------------------------------------------------------------------------------------------------------------
>
>
>
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to