[Rails] Re: Better way to count Active Record Data?

2010-01-27 Thread Michael Kahle
Marnen Laibow-Koser wrote:
> Michael Kahle wrote:
>> I am trying to get hourly counts of orders between different price
>> ranges from my database.  I'm thinking there has got to be a better way
>> than looping through each record and checking if the created_at time
>> fits between hour 1, 2, 3, 4, etc of the day and then returning the
>> count.
> 
> You are correct.  Use the database to your advantage.  Why would you be 
> looping through records and checking created_at in the first place? 
> That's what :conditions is for!
> 
>> 
>> Currently I am displaying just a total of the days sales that are
>> between different dollar amounts this way:
>> 
>> In my controller I grab all orders that are from today and return those
>> to the view as @orders
>> 
>> In my view I display the number sold that are in a particular price
>> range by the following helper method:
>> 
>>   def number_between_75_and_100_sold
>> count = 0
>> for n in @orders
>>   if n.total >= 75 && n.total < 100
>> count = count + 1
>>   end
>> end
>> return count
>>   end
>> 
>> This helper method feels ugly to me.  I have 3 others that give me
>> different order total ranges.  Better way?
>> 
>> If I use this same logic to count sales per hour I am going to end up
>> with 24 more helpers that count those sales for each hour by looking at
>> created_at times between a range.  This is going to be slow, ugly and
>> lame.
> 
> It certainly is.  Learn about SQL aggregate functions, and their 
> abstraction layer (ActiveRecord::Calculations).  What you want is 
> something like
> 
> Order.count :conditions => ["created_at >= ? and total between ? and ?", 
> 24.hours.ago, 75, 100]
> 
> That's one SQL query that will only fetch the data you need.

Thanks for your reply.  I use :conditions in my query to setup the 
@order.  I was just thinking that it would be worse to do a bunch of 
lookups to the database for each dataset I am looking for when I could 
just fetch all the records I need once and then loop through @order to 
find what I need.

I have it in my head that 1 query to the database is better than 24 to 
get the data I need for sales each hour of the day.

Is that not correct?

> 
>> 
>> I appreciate any advice.
> 
> Best,
> --
> Marnen Laibow-Koser
> http://www.marnen.org
> mar...@marnen.org

-- 
Posted via http://www.ruby-forum.com/.

-- 
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 rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.



[Rails] Better way to count Active Record Data?

2010-01-27 Thread Michael Kahle
I am trying to get hourly counts of orders between different price
ranges from my database.  I'm thinking there has got to be a better way
than looping through each record and checking if the created_at time
fits between hour 1, 2, 3, 4, etc of the day and then returning the
count.

Currently I am displaying just a total of the days sales that are
between different dollar amounts this way:

In my controller I grab all orders that are from today and return those
to the view as @orders

In my view I display the number sold that are in a particular price
range by the following helper method:

  def number_between_75_and_100_sold
count = 0
for n in @orders
  if n.total >= 75 && n.total < 100
count = count + 1
  end
end
return count
  end

This helper method feels ugly to me.  I have 3 others that give me
different order total ranges.  Better way?

If I use this same logic to count sales per hour I am going to end up
with 24 more helpers that count those sales for each hour by looking at
created_at times between a range.  This is going to be slow, ugly and
lame.

I appreciate any advice.
-- 
Posted via http://www.ruby-forum.com/.

-- 
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 rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.



[Rails] Suggestions for a rails webhost...

2009-01-29 Thread Michael Kahle

I have multiple domains I want to move to a rails host.  I would like
full SSH access to the servers, ftp, and a knowledgeable support staff
that I can get on the phone.

Any suggestions?
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: Fukuoka Ruby Award--about $10,000 for first prize

2008-12-15 Thread Michael Kahle

By no means have I learned enough Ruby ninjutsu to dream of entering a 
Ruby competition.  That said, what does this mean?

>From the link:
=begin
Eligibility
...
Type B is for activities for prevalence.
...
Specify URL to confirm for operation check in the first selection except 
the case of activities for prevalence which haven’t actuating system.
=end

Huh?  What does this even mean?
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: Saving a "final" version of a record.

2008-12-15 Thread Michael Kahle

Thank you Robert and Patrick for such thought provoking posts.  Notice 
how both of you are saying similar things to what Ar first suggested?

> Ar called it a "before_save callback function."
>> Robert suggested, "I suppose I could have done the update 
>> automatically on save depending on the state of the claim.
>> But, I chose instead to allow the user to update at their 
>> own discretion."
>>> Finally Patrick thought of the copy-on-write feature.

This got me thinking about rsync, or git; even Apple's Time Machine. 
All of these programs store data in a "state" that allows you to view 
the changes over a period of time.  What is this design pattern called? 
Has it been written about?

The software we use to run the business where I am employed has 
something like this on the accounting side.  The whole, "close out the 
month", feature.

Humm...
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Saving a "final" version of a record.

2008-12-14 Thread Michael Kahle

I'll start by apologizing for the title.  I can't think what to call
this.

I have some Claims that need to be entered, but when they are "closed"
it saves the data from other related tables into the Claim (this could
get messy), so that if the Customer, Registration, or Dealer information
is changed at a later date it will remain the same as when the Claim was
closed.

Is there a preferred method for doing this?  Some kind of plugin or
something?

I understand I could have a separate table for keeping the "archived"
version of the record, but I fear the work to create all new tables and
then storing the record as this other new archive type which is nothing
more than a duplicate of the Claim.

Any thoughts?

P.S. - Ask away, I am probably not explaining this perfectly, but I will
try to elaborate if you are having trouble understanding my challenge.
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: Returning changed fields when transaction fails...

2008-11-04 Thread Michael Kahle

>Mark Reginald James wrote:
> Yes, it's possible to eliminate the explicit validity
> checks and jump straight into the transaction, allowing
> the validations to happen automatically during calls to
> save! or update_attributes!.
> 
> The disadvantage of this is that if the second object saved is
> invalid, the save of the first object has to be rolled-back,
> which is significantly slower than checking the validity of
> both before hitting the database. You also have to add that
> rescue block to ensure that the validity of the second object
> is checked for purposes of form error display.

Perfect!  Thanks so much for your patience and very helpful answer to my 
problem.

Michael
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] Re: integration with plone site--insane?

2008-11-03 Thread Michael Kahle

>Roy Pardee wrote:
> Has anybody tried to integrate a plone site & a ror app?  Single-sign-on 
> is the most desired thing I believe, tho sharing lists of people & 
> contact info etc. would also be nice.  Any pointers/advice would be most 
> appreciated.

What about OpenID?  On the plone side have them add OpenID support.
http://plone.org/documentation/how-to/openid-support

On the RoR side do the same.
http://wiki.rubyonrails.org/rails/pages/openid

Cheers.
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] Re: Returning changed fields when transaction fails...

2008-11-03 Thread Michael Kahle

>Mark Reginald James wrote:
>> Michael Kahle wrote:
> 
>> errors.
> The "!" just causes an exception to be raised if the record
> is invalid, or if saving was prevented by a callback.

Ok.

> Here you've already checked that both the records are valid,
> so you just want to ensure that any other problem causes
> the propagation of an app-wide exception (which will also
> rollback the transaction). Also, you have already updated
> the records from the user-form using attributes=.

Correct.  When you say, "Here you've already checked that both the 
records are valid...", you must be referencing the way you coded it.  I 
don't think I'm checking it any time before I run the "update.bla.bla!" 
method.  See my other post.  I think I do not understand when Rails does 
it's checking automatically.

> Therefore you want to use save_without_validation! rather than
> update_attributes!, save!, or save.

Sure. If I can get it straightened out, when Rails is doing its 
non-explicit checking; via - valid?, .save!, update.bla.bla! or 
otherwise, I am sure you are correct here and would see a (perhaps only 
minor, but still better!) performance upgrade.
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] Re: Returning changed fields when transaction fails...

2008-11-03 Thread Michael Kahle

>Mark Reginald James wrote:
>> Michael Kahle wrote:
>> getting over my head! :)
> If a model before_validation, after_validation, or before_save
> method or block returns false, a valid record won't be saved,
> but you only get an exception if the saving method ended with
> an exclamation mark.

I understand.  What I don't understand is: when is the model 
automatically checked?  Anytime you submit the form back to the 
controller?

>> Are you saying that anything caught by "rescue" is something that you 
>> wouldn't want to pass back to the user?  Is this because, let's say, in 
>> the event of a database failure it would better to send this to a log 
>> file or something similar?  Is the idea behind what you are saying that 
>> we should only be passing validation errors back to the user?
> 
> While you can use rescue sections in individual actions to
> specially handle and inform users about action-specific errors,
> to prevent duplication it's usually best to have other exceptions
> handled by a single method in application.rb that informs both the
> user and the developer than an unexpected error has occurred.
> e.g. See the exception_notification plugin.

Nice advice.  I'll check out that plugin in the next few days.  I need 
to get this iteration complete.  :)
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] Re: Returning changed fields when transaction fails...

2008-11-02 Thread Michael Kahle

>Mark Reginald James wrote:
>> Michael Kahle wrote:
> 
>> Ooo.  Another thought.  I noticed that you are doing a transaction 
>> without using the "rescue" catch.  If something else goes wrong, besides 
>> validation, you will never know what happened!  :)
> 
> Unless you have a pre-save model callback preventing the save for
> a specific reason by returning false, that you wish to signal to
> the user, such exceptions are better handled by an app-wide catcher.

I'm really not sure what a pre-save model callback is.  This is rapidly 
getting over my head! :)

Are you saying that anything caught by "rescue" is something that you 
wouldn't want to pass back to the user?  Is this because, let's say, in 
the event of a database failure it would better to send this to a log 
file or something similar?  Is the idea behind what you are saying that 
we should only be passing validation errors back to the user?

This would make sense to me.  I'm just far too green with Ruby at this 
point to even think about that.

Thanks again.
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] Re: Returning changed fields when transaction fails...

2008-11-02 Thread Michael Kahle

>Mark Reginald James wrote:
>> Michael Kahle wrote:
>> Another good thought, however what I am using now doesn't have the same 
>> duplication as your suggested code does.  Unless of course when I call 
>> the "attributes" method it runs a check then as well.  I'm not sure that 
>> is the case.  I'll have to think about that.
> 
> It's the validation code that's run twice, once at valid? and
> once at save!, which you'll see if you put a logger.debug call
> in say a Customer.validate method.
> 
> This usually doesn't cause problems, only unnecessary extra
> CPU load.

Ok.  I'm confused.  If I step through my code line by line it would seem 
to me that it does the following:

1.) Creates a new object to dump stuff into.
2.) Dumps everything from the form (params) into these new objects.
3.) Begins the transaction.
4.) First updates @snowplow_registration, the ! at the end of the update 
method forces a check against the model for validation and returns any 
errors that come from the update.  If it doesn't succeed, then it is 
caught by the rescue case and then checks the @customer object for 
errors.

Does this sound right?  It sounds like I might be missing something 
fundamental here.

Thanks for looking.  :)
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] Re: Getting started with ROR when you come from the desktop world with no experience in developing

2008-11-02 Thread Michael Kahle

Andy wrote:
> Buy the Ruby Pickaxe book.
> Buy Agile Web Development with Rails (wait for next edition)
> Find a hosting company that supports RoR if you need hosting. Use
> mod_rails to deploy.

I bought both of these books as PDF's from the Pragmatic Programmers 
site.  I have dual monitors so I really liked this setup.  Textmate in 
one window and my PDF in another.

I also have access to a color printer and a binder at work.  So I 
printed out the Active Record, Action Controller, and Action View 
chapters and bound them into 3 separate little books.  They fit nicely 
into my laptop case and has been a really neat tool to help me reinforce 
what is going on in these 3 important pieces of rails.  I have to think 
about the MVC design pattern to grab the right book to find what I am 
looking for.

The 3rd edition is out in PDF as a beta from their site now if you want 
to get started; same with the new Pickaxe book.

Good luck!  I'm having a great time.  :)
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] Re: Returning changed fields when transaction fails...

2008-11-02 Thread Michael Kahle

Michael Kahle wrote:
> Mark Reginald James wrote:
>> Michael Kahle wrote:
>> 
>> 
>>> Super!  Thanks so much.  It worked perfectly.  The only thing I changed 
>>> from your example was to handle the update as a transaction.  Updating 
>>> @registration and @customer with the ".attributes" method did the trick 
>>> perfectly.  I am off to experiment with the "validates_associated 
>>> :customer".
>> 
>> Could you explain a little more what you changed and why.

Ooo.  Another thought.  I noticed that you are doing a transaction 
without using the "rescue" catch.  If something else goes wrong, besides 
validation, you will never know what happened!  :)

>  if @snowplow_registration.valid? && customer_valid
>SnowplowRegistration.transaction do
>  @snowplow_registration.save!
>  @customer.save!
>  flash[:notice] = 'Snow Plow Registration was updated.'
>  redirect_to(@snowplow_registration)
>end
>  else
>render :action => :edit
>  end

So in that case, I prefer a sprinkle of your code with mine.  Well, to 
be honest I probably lifted this off of one of the pages in my Agile Web 
Development with Rails book.  So I won't take any credit for this.  I 
wish I could!  Cheers.
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] Re: Returning changed fields when transaction fails...

2008-11-02 Thread Michael Kahle

Mark Reginald James wrote:
> Michael Kahle wrote:
> 
> 
>> Super!  Thanks so much.  It worked perfectly.  The only thing I changed 
>> from your example was to handle the update as a transaction.  Updating 
>> @registration and @customer with the ".attributes" method did the trick 
>> perfectly.  I am off to experiment with the "validates_associated 
>> :customer".
> 
> Could you explain a little more what you changed and why.

I will do my best; it certainly helps to re-enforce what I've learned.

In your example you provided, what was to me, a novel idea in terms of 
updating the @snowplow_registration and @customer objects.  When these 
objects were first created in the update method, they had the properties 
of a newly fetched record from the database.  It wasn't until I called 
on each of these:
  @snowplow_registration.update_attributes!(params[:snowplow_registration])
  @customer.update_attributes!(params[:customer])
respectively, did the newly created objects, instantiated at the 
beginning, take on the properties of my submitted form.  My problem was 
that when the transaction failed during the first update, I would never 
see the newly created @customer object updated with my form information. 
By calling the ".attributes = params[]" method on each of these BEFORE I 
started my transaction I now had in memory the new form data I entered 
to pass to my rescue catch.  In your code, you check the Customer object 
before submitting it to be written.

I was about to write about how important it was for me to do this update 
in a transaction because of the possibility of the database failing in 
between writes and how your code didn't account for that.  I glanced 
over your code when I saw:
  if @snowplow_registration.valid? && customer_valid
and simply assumed you were doing a standard non-transactional save to 
the database.  Now that I'm inspecting this more closely, I see that you 
did-in-fact keep this a transactional operation.  Here is the code I am 
using now:

  def update
@snowplow_registration = SnowplowRegistration.find(params[:id])
@customer = @snowplow_registration.customer

@snowplow_registration.attributes = params[:snowplow_registration]
@customer.attributes = params[:customer]


SnowplowRegistration.transaction do
  @snowplow_registration.update_attributes!(params[:snowplow_registration])
  @customer.update_attributes!(params[:customer])
  flash[:notice] = 'Snow Plow Registration was successfully 
updated.'
  redirect_to(@snowplow_registration)
end

  rescue ActiveRecord::RecordInvalid => e
@customer.valid?
render :action => "edit"
  end

As you can see the only changes I've made from my first post are:

1) I originally had in my 3rd line:
  @customer = Customer.find(@snowplow_registration.customer)
   and changed it to what you had:
  @customer = @snowplow_registration.customer
   I thought your way looked cleaner.

2) I then added:
  @snowplow_registration.attributes = params[:snowplow_registration]
  @customer.attributes = params[:customer]
   as I explained above, this did the trick to update BEFORE
   my transaction occurred, the @customer and @registration classes.

I had to change nothing else from my original example as the checks are 
already in place.

> One little improvement: Use save_without_validation! to prevent
> the validations from being run twice.

Another good thought, however what I am using now doesn't have the same 
duplication as your suggested code does.  Unless of course when I call 
the "attributes" method it runs a check then as well.  I'm not sure that 
is the case.  I'll have to think about that.

Thank you so much for this discussion and all the wonderful new tidbits 
you've showed me.  Like I said above, this discussion just helps 
reinforce the things that I am learning.
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] Re: Returning changed fields when transaction fails...

2008-11-02 Thread Michael Kahle

Mark Reginald James wrote:
> Michael Kahle wrote:
>>   
>> @snowplow_registration.update_attributes!(params[:snowplow_registration])
>> 
>> When the transaction starts, if validation fails on the
>> @snowplow_registration, it doesn't check the validation on my @customer
>> for the fields that were updated.  Validation for both works properly
>> when testing separate... but @customer returns to the edit page without
>> passing the changed (params[:customer]) because the failure in the
>> transaction happened before I can call
>> @customer.update_attributes!(params[:customer]).  Is there a way to
>> update the model in memory before updating the database?
> 
> This is one way:
> 
>def update
>  @snowplow_registration = SnowplowRegistration.find(params[:id])
>  @customer = @snowplow_registration.customer
> 
>  @snowplow_registration.attributes = params[:snowplow_registration]
>  @customer.attributes = params[:customer]
> 
>  customer_valid = @customer.valid?
>  if @snowplow_registration.valid? && customer_valid
>SnowplowRegistration.transaction do
>  @snowplow_registration.save!
>  @customer.save!
>  flash[:notice] = 'Snow Plow Registration was updated.'
>  redirect_to(@snowplow_registration)
>end
>  else
>render :action => :edit
>  end
>end
> 
> If you add "validates_associated :customer" to the
> SnowplowRegistration class you only then need to check
> the validity of @snowplow_registration, but the validity
> of the customer will now be checked whenever a
> SnowplowRegistration instance is saved.

Super!  Thanks so much.  It worked perfectly.  The only thing I changed 
from your example was to handle the update as a transaction.  Updating 
@registration and @customer with the ".attributes" method did the trick 
perfectly.  I am off to experiment with the "validates_associated 
:customer".

Thanks again.

Michael
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] Returning changed fields when transaction fails...

2008-11-01 Thread Michael Kahle

I hope I'm explaining this correctly.  Be gentle.  Noob here.  :)

Ideas on how to solve this?  Kind of a chicken or the egg scenario.
Check out this update function in my controller:

  def update
@snowplow_registration = SnowplowRegistration.find(params[:id])
@customer = Customer.find(@snowplow_registration.customer)

SnowplowRegistration.transaction do
  @snowplow_registration.update_attributes!(params[:snowplow_registration])
  @customer.update_attributes!(params[:customer])
  flash[:notice] = 'Snow Plow Registration was successfully
updated.'
  redirect_to(@snowplow_registration)
end

  rescue ActiveRecord::RecordInvalid => e
@customer.valid?
render :action => "edit"
  end

When the transaction starts, if validation fails on the
@snowplow_registration, it doesn't check the validation on my @customer
for the fields that were updated.  Validation for both works properly
when testing separate... but @customer returns to the edit page without
passing the changed (params[:customer]) because the failure in the
transaction happened before I can call
@customer.update_attributes!(params[:customer]).  Is there a way to
update the model in memory before updating the database?

Thank you for looking.
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] Manipulating/Accessing objects that are :polymorphic => true

2008-10-25 Thread Michael Kahle

I am working on an application to track warranty registrations for my
employers product line.  The product line includes the following
products:

spreaders
snow_plows
skid_steers

Each of these products have different information that I have to track.
The tables would look something line this:

create_table :spreaders do |t|
  t.integer :registration_id
  t.string :spreader_sn
  t.string :spreader_model_number

create_table :snow_plows do |t|
  t.integer :registration_id
  t.string :power_pack_sn
  t.string :control_package_sn
  t.string :blade_sn

create_table :skid_steers do |t|
  t.integer :registration_id
  t.string :plow_sn
  t.string :plow_control_sn
  t.string :blade_sn

Notice how each of these products need to collect different information.
They are all registrations so I can create table to hold these
registrations with information that is common to them all.

create_table :registrations do |t|
  t.integer :customer_id
  t.integer :dealer_id
  t.date :date_purchased
  t.integer :vehicle_id
  t.text :comment
  t.integer :registerable_id
  t.string :registerable_type

I have the models defined like so:

class Registration < ActiveRecord::Base
  belongs_to :customer
  belongs_to :dealer
  belongs_to :vehicle
  belongs_to :registerable, :polymorphic => true
end

class Spreader < ActiveRecord::Base
  has_many :registrations, :as => :registerable
end

class SnowPlow < ActiveRecord::Base
  has_many :registrations, :as => :registerable
end

class SkidSteer < ActiveRecord::Base
  has_many :registrations, :as => :registerable
end

At this point I am trying to simply display a record on the registration
view and can't seem to understand how I would access this.  Here's my
registration view:

1  Listing registrations
2
3  
4
5  Customer Name
6  Dealer Name
7  Date purchased
8  Product
9  Product SN
10 Vehicle
11 Comment
12   
13
14   <% for registration in @registrations %>
15   
16 <%=h registration.customer.full_name %>
17 <%=h registration.dealer.dealer_name %>
18 <%=h registration.date_purchased %>
19 <%=h registration.registerable_type %>
20 <%= registration.registerable_type.id %>
21 <%=h registration.vehicle.model %>
22 <%=h registration.comment %>
23   
24   <% end %>
25 
26
27 <%= link_to 'New Registration', new_registration_path %>

On lines 16, 17 and 21, I am able to display the full_name of the
Customer, likely because my Registration belongs_to the Customer.  I am
also able to see the dealer_name of the Dealer, similarly because my
Registration belongs_to the Dealer.  And finally the model of the
Vehicle, again because of the magical belongs_to declaration.
Wonderful.  On line 20 is where I loose my mind.

I'm thinking there should be some similar magic going on behind the
scenes to view the product (Spreader, SnowPlow, or SkidSteer) of the
type listed in the registerable_type field because of my nifty:
belongs_to :registerable, :polymorphic => true
declaration in the Registration model.  How would I access this record?
It seems to me this functionality needs to be customized in the
RegistrationsController because unlike the simple foreign key
relationship Customers and Dealers enjoy with Registrations Rails does
not know how to handle this polymorphic relationship by default.  Or,
which is probably more likely, I have no idea what I'm talking about and
Rails DOES magically pull in the correct table because of that
association.  Ideas?

I can understand how to do this if I were to do this entirely
differently by changing my design.  For instance creating three new
models called SpreaderRegistration, SnowPlowRegistration and
SkidSteerRegistration and just throwing the whole idea of polymorphic
relationships out the window.  But it introduces duplication and I would
have to maintain three separate MVC's for each.  Seems silly and not at
all extensible.  What if I have to add a new product that needs to be
registered?  Not I good idea I think.

There are other considerations I need to think about as well; such as
how would I tell the Registration it is registerable_type Spreader so it
can create a new registration form that loads in the fields I need for
the Spreader as well as the fields for all registrations.  I am getting
ahead of myself.

This MUST be a common problem.  Can someone point me to a good tutorial
that deals with this?  I've found others that talk about polymorphic
types and give migration and model examples, but nothing for the view or
controller.

Thanks for your thoughts and time.
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
To unsubscribe from t

[Rails] Re: [JOB] - Milwaukee/Chicago Area Short 2 day contract...

2008-10-24 Thread Michael Kahle

> Please send me an email if you're interested.  Provide links to Rails
> work you've done in the past and a short paragraph on why you think you
> would be a good tutor for me.  I will also answer any questions you may
> have.

You probably want my email... *sigh*.

geopoliticus<-at->gmail.com.remove-me
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] [JOB] - Milwaukee/Chicago Area Short 2 day contract...

2008-10-24 Thread Michael Kahle

Hello.  I am working, unsuccessfully, to complete a small and simple
Warranty registration and warranty claims database application for our
product line.

I have clearance from my superior to hire for ~ 20 hours of work someone
who is a Rails guru to assist me in this creation.  I should clarify
that, this person would be using me to help them define the requirements
and I would be tagging along to use this persons knowledge to help bring
me up to speed on Rails in the process.

I would be willing to come to the Milwaukee or Chicago area to spend a
few days working with you on this project / lesson.  Denver or New York
is not out of the question either for the right candidate.

The person would have to have an understanding of polymorphic
relationships and their use when setting up simple model, views and
controllers.  If you can't bang something out quickly like this, need
not apply.  The interface does not have to be pretty, it's more
important that I gain an understanding of this process so that I can
extend the application in the future.

I will be paying hourly for this service and would like to reserve a 20
hour time block over the period of 2 days.  The hourly rate is
negotiable, for the right person we would be willing to pay well.

Think of this as a personal tutor with a pre-defined sample lesson.  :)
It would be a fun few days for some easy money for the right person.
I'm easy going and a fairly quick study.

Please send me an email if you're interested.  Provide links to Rails
work you've done in the past and a short paragraph on why you think you
would be a good tutor for me.  I will also answer any questions you may
have.

I'm reading this over and I think this sounds like a strange request.
:)
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] Re: has_many relationship able to be placed on one to others?

2008-10-24 Thread Michael Kahle

Bed time again.  Strange how hyper-focused I can be at night.

I've been working off of a post to get me going on understanding how to 
implement in Rails these polymorphic associations.
http://railsforum.com/viewtopic.php?id=5882

I also found this:
http://www.pathf.com/blogs/2008/07/drying-up-rails-controllers-polymorphic-and-super-controllers/
Looks promising, but at this level of abstraction I have trouble 
understanding.

I'm afraid I'm trying to run before I can walk.
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] Re: has_many relationship able to be placed on one to others?

2008-10-23 Thread Michael Kahle

Michael Kahle wrote:
> I don't think this relationship would work.  Because we are dealing
> with serialized inventory.  So product A is really an assembly that
> consists of several different serialized parts and can only be
> registered once to 1 customer.  Product B is also an assembly that
> consists of several different serialized parts that can only be
> registered to a single customer.  The customer can have many different
> products all with unique registrations.  Does that help?

Shows what I know... nothing.  I have been struggling with this for the 
past few hours (slow learner?), but upon further review I believe this 
is exactly what I need.

I have built the relationships, but now I am trying to find examples on 
how I would setup the controller for each product to handle the 
registration.

I'll post again when I know more, but thanks for this.  I believe it is 
precisely the direction I should be going.
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] has_many relationship able to be placed on one to others?

2008-10-23 Thread Michael Kahle

It's time for me to go to bed... but perhaps I'll wake up to a bit of
good advice.

I have 3 tables:
  registrations
  cars
  motorcycles

I am trying to define the registration object in this way:
  has_many :cars
  has_many :motorcycles

Then for the cars and motorcycles I'm saying
  belongs_to :registration

Something is quite wrong here.  How should I be relating these?  I read
this tutorial:
http://railsguides.phusion.nl/activerecord/association_basics.html

I keep referencing back to this, but I cannot find a relationship that
fits.

I'm sure I'm not understanding something properly about how
relationships should work.  I just can't wrap my head around the "right
way" to do things when setting up relationships.  Frustrating.  Good
night all.
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] Re: Returning a record's ID before the record is made?!??

2008-10-22 Thread Michael Kahle

Craig Demyanovich wrote:
> In their example,
> ... [snip]
> they assign the product to the details, @details.product = @product. 
> When
> you do that with related ActiveRecord objects, ActiveRecord takes care 
> of
> setting the IDs for you. In this case, @details.product_id will be set 
> to @
> product.id.
> 
> Regards,
> Craig

Thanks for your post Craig.  I could not wrap my head around this 
concept, partially because it was so difficult for me to implement in 
JSP that I resorted to using randomly generated UUID's before inputing 
the record into the database.

Active Record to the rescue indeed.  Wow.  I spent about an hour playing 
with this last night and was able to magically get this to, well, just 
work in Rails.

When I wrote this post originally I hadn't yet tried the example as I 
just couldn't understand the code and how it worked.  I took the leap of 
faith and followed the example and Ta-da, onto more interesting 
problems.

I should also not that another user wrote me off-list and I'd like to 
thank him/her for the wonderfully helpful response.  I am not mentioning 
his/her name in the event that they wrote off list because they wanted 
to remain anonymous.  But thank you.  They suggested I read:
http://railsguides.phusion.nl/activerecord/association_basics.html
and it helped immensely.  I felt like I missed a meeting!

Ruby/Rails has been such a fun experiment thus far.  I'm really loving 
it's elegance and simplicity.  Thanks again to all.  TTFN.
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] Re: Cleaner way to build a "one or the other" validation?

2008-10-22 Thread Michael Kahle

Roy Pardee wrote:
> How about:
> 
> def validate_dealer_id
>   if number.blank? then
> errors.add_to_base("You must specify either a Dealer Number OR a Sub 
> Dealer Number.") if subnumber.blank?
>   else
> erros.add_to_base("A dealer can't have both a dealer number AND a 
> sub dealer number") unless subnumber.blank?
>   end
> end
> 
> ?
> 
> Extra bonus kibbitzing:
>  - Would it make things any easier to store both those
>numbers in a single field, and keep track of whether
>a given dealer is a dealer or a sub in a separate 'dealer_type'
>field?
>  - FWIW--the ruby convention is that methods whose names
>end in a ? return a boolean.
> 
> Cheers,
> 
> -Roy

I don't think that your above example is any cleaner then my original 
solution.  However, your bonus kibbitzing (lol) I think is a really good 
thought.  I am going to implement this logic on a later iteration. 
Thanks for this.
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails] Re: Cleaner way to build a "one or the other" validation?

2008-10-22 Thread Michael Kahle

Frederick Cheung wrote:
> Use the Xor operator ( ^ ) ?
> 
> Fred

Sexy.  Thanks.

I changed above to:

class Dealer < ActiveRecord::Base
  validate :dealer_id?

private
  def dealer_id?
if !(number.blank? ^ sub_number.blank?)
  errors.add_to_base("You must specify either a Dealer Number OR a 
Sub Dealer Number.")
end
  end
end

Makes me feel less dirty.
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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 rubyonrails-talk@googlegroups.com
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
-~--~~~~--~~--~--~---