[Rails] Edit With Nested Forms - Values Not Preserved Across Submit
When a nested form submitted to the create action contains missing data, the form is redisplayed and the nested values the user entered remain. When submitting the same nested form to the update action, the newly entered values remain for the parent record, but the child records revert back to their pre edit values. Here's the form (from Rails Guides): % form_for @customer do |customer_form| % div %= customer_form.label :name, 'Customer Name:' % %= customer_form.text_field :name % /div % customer_form.fields_for :orders do |order_form| % p div %= order_form.label :number, 'Order Number:' % %= order_form.text_field :number % /div % unless order_form.object.new_record? % div %= order_form.label :_delete, 'Remove:' % %= order_form.check_box :_delete % /div % end % /p % end % %= customer_form.submit % % end % It looks like this is a problem with accepts_nested_attributes_for. It loads and updates the records in the nested form, but when the form is redisplayed @customer.orders is loaded from the DB, squashing the changes by the nested attributes module. Is it possible to retain nested data across form redisplays to the update action? -- 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: Edit With Nested Forms - Values Not Preserved Across Submit
On Mar 25, 12:07 am, MaggotChild hsomob1...@yahoo.com wrote: Is it possible to retain nested data across form redisplays to the update action? Anyone? The answer appears to be no, given the Rails Guides example and the Complex Forms Examples: github.com/alloy/complex-form-examples/tarball/ master. This is a big downside to using nested attributes with complex forms. -- 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] Rails Strives For DRY. A Lot of REST Routes, Not So DRY.
Most in the Rails community seem stick with 1 controller per resource when using RESTfull routes. This makes sense for a resource with one scope and one view, but once a resource is used with multiple scopes and/or views the amount of code duplication needed to manage these in a single controller can balloon quick. Yet despite the impending chaos, it seems as though people prefer to maintain a controller per resource. Take these examples from Rails sites written by well respected authors: 1. http://tinyurl.com/2d9rl3v find_container() and find_events(): why would one want to do this? This can become unmanageable. If each of these scopes require different view components the same checks will have to be done in the action's view or when determining the layout. 2. http://tinyurl.com/2679ye3 Look at how many times wall? and blog? have to be called. Not very DRY if you ask me. Rail's routing gives you these checks for free, why not create a WallCommentsController and BlogCommentsController? If differing views are involved for each of these it seems like a no brainer... but it's never done. Why? What am I missing? -- 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] Users Uploading Files To An asset_host
I'm curious how people put static files uploaded by users to their asset host. I'd think (for non S3, CloudFiles, etc...) that the files would be submitted to the app server which would save them to a shared drive on the asset host. I've head bad things about NFS and high volume sites. Is Samba is an acceptable alternative? Putting part of the app on the asset host and submitting directly to it makes some sense, though it somewhat defeats the separation of app server, asset host, etc... How are you guys handling this? -- 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] HABTM collection_ids: confirm changes before saving
It seems that when a HABTM (or has many) is involved, having a confirmation page prior to saving doesn't work as seamlessly as other Rails view/model parings, specifically when confirming an edit. This is caused by the immediate inserts or deletes that take place upon assigning to collection_ids. Changes are saved regardless of whether the user wants to cancel or proceed. select_tag 'product[provider_ids][]', collection_options_with_selected #pseudo def preview @product = Product.find params[:id] @product.attributes = params[:product] #provider_ids saved here if @product.valid? render 'confirm; else render 'edit' end end build is not appropriate and push has the same functionality as assigning to provider_ids. So I have to do this: def preview @product = Product.find params[:id] @product.attributes = params[:product] #no product @selected_providers = Provider.find_by_ids params[:provider_ids] #to display provider names in confirm page #... end def update @product = Product.find params[:id] @product.attributes = params[:product] @provider_ids = params[:provider_ids] if @product.valid? Product.transaction do @product.provider_ids = @provider_ids @product.save end else ... end There's no way to do this in the typical Rails attribute assignment fashion? -- 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] Re: HABTM collection_ids: confirm changes before saving
On Dec 31, 11:08 am, MaggotChild hsomob1...@yahoo.com wrote: def preview @product = Product.find params[:id] @product.attributes = params[:product] #no product I meant that no provider_ids are included in params -- 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] Using current_page? With An Edit Route
current_page?(:action = 'edit', :controller = 'admin/products') fails with an ActionView::TemplateError (No route matches ...) unless I'm in the edit action or I provide the :id. Route: /admin/products/:id/edit(.:format) {:action = 'edit', :controller = 'admin/products'} I understand why this happens, but is there a way to make current_page? check for edit routes without specifying an id? Thanks -- 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] db:test:purge And osql: What Happened?
I thought I had read somewhere why db:test:purge continues to use `osql` but all I could find is this: http://dev.rubyonrails.org/ticket/7054 Obviously this never made its way into Rails. Why? What are people doing for MS SQL testing on non Windows OS'? Everyone's hacking their own workaround? Seems as if there would be a universal antidote. -- 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] Has Many Relationship: collection_ids= Method Useless?
When modifying a model that has a has_one or has_many relationship people usually create form that will submit the id of the selected associated object. Rails takes this into consideration with validations: validate_presence_of :country will validate if country or country_id has been set. In Rails, providing the associated ID works fine with has_one but if you try to do this with a has_many: form_for(@country) do |form| form.select :state_ids, @states form.select :state_ids, @states #etc... The selected state_ids are written to the DB as soon as they're assigned. What's the reasoning behind this? How is one supposed to assign multiple collection ids via a form? accepts_nested_attributes_for doesn't work out too well either because you're just setting IDs -a field that is explicitly ignored by accepts_nested except when testing whether or not the given params are new. --~--~-~--~~~---~--~~ 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: Has Many Relationship: collection_ids= Method Useless?
On Nov 14, 2:42 am, Leonardo Mateo leonardoma...@gmail.com wrote: On Sat, Nov 14, 2009 at 6:37 AM, MaggotChild hsomob1...@yahoo.com wrote: In Rails, providing the associated ID works fine with has_one but if you try to do this with a has_many: form_for(@country) do |form| form.select :state_ids, @states form.select :state_ids, @states #etc... The selected state_ids are written to the DB as soon as they're assigned. What's the reasoning behind this? How is one supposed to assign multiple collection ids via a form? You have to send them as an array, and assign them as an array too. For example, View form.select(state_ids[], @states) form.select(state_ids[], @states) Controller @whatever.state_ids = params[:state_ids] That's the problem. As soon as you make that assignment the IDs are written to the db. You'd have to say: Whatever.tranaction do @what = Whatever.new(params[:whatever]) #other stuff if @what.valid? end Which can result in an unnecessarily large transaction scope. Though this is somewhat expected since and co. also write to the DB on assignment. --~--~-~--~~~---~--~~ 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] Nested Attributes For HABTM - Deleting The Right Record
In a many-to-many relationship, defined via has_and_belongs_to_many, accepts_nested_attributes_for deletes the wrong record(s) when marked_for_destruction? == true. For example: class Ho AR has_and_belongs_to_many :strolls, :join_table = :ho_strolls accepts_nested_attributes_for :strolls, :allow_destroy = true end class Stroll AR has_and_belongs_to_many :hos, :join_table = :ho_strolls end attribs = {1={_delete=1, :id=123}} h=Ho.first h.strolls_attributes = attribs h.save! This will delete Stroll with an id of 123 from the join table and the strolls table. This is wrong. It should only be deleted from the ho_strolls table. Though I believe there is a fix (or a debate still, maybe) on the way: https://rails.lighthouseapp.com/projects/8994/tickets/2251-associationcollectiondestroy-should-only-delete-join-table-records At any rate, I have the following work around. It assumes that I'll never delete a stroll from the strolls tables via a hoe. What do you think? class Ho AR has_and_belongs_to_many :strolls, :join_table = :ho_strolls accepts_nested_attributes_for :strolls, :allow_destroy = true def before_save remove_marked_strolls end def remove_marked_strolls marked = strolls.select { |stroll| stroll.marked_for_destruction? } marked.each { |stroll| stroll.delete(stroll) } #delete from join table only end end I though about doing: class Stroll AR ... def unmark_for_deletion; @marked_for_deletion = false; end end And then unmarking in remove_marked_strolls, though this seems superfluous since the records are remove from the collection the proxy will never get to check for this condition and delete them from strolls. --~--~-~--~~~---~--~~ 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: Select Option Broken on Eager Load With 2nd Order Association
On Oct 15, 12:22 am, Frederick Cheung frederick.che...@gmail.com wrote: On Oct 15, 3:13 am, MaggotChild hsomob1...@yahoo.com wrote: dont_select_me is included. The docs say nothing of about :select being stepped on by fall back left joins (not the :include!). This seems like a bug Well the docs should say that - it has always been that way. You need something likehttp://www.kellogg-assoc.com/2006/11/05/eager-finder-sql Thanks, that's what I need. --~--~-~--~~~---~--~~ 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] Select Option Broken on Eager Load With 2nd Order Association
I only want to select certain columns when eager loading an association: class Package ActiveRecord::Base has_many :deliveries, :select='id, name, region_id, package_id', #exclude text column dont_select_me :include=:region end class Delivery ActiveRecord::Base belongs_to :package belongs_to :region end Package.first :include = :deliveries Generates the correct SQL: SELECT TOP 1 * FROM [packages] SELECT id, name, region_id, package_id FROM [deliveries] WHERE ([deliveries].package_id = 1) SELECT * FROM [regions] WHERE ([regions].[id] = 1) But, when I say: Package.first :include = :deliveries, :conditions = deliveries.name = 'shlomo' The SQL is: SELECT [packages].[id] AS t0_r0, [packages].[name] AS t0_r1, [packages].[created_at] AS t0_r2, [packages].[updated_at] AS t0_r3, [deliveries].[id] AS t1_r0, [deliveries].[name] AS t1_r1, [deliveries].[created_at] AS t1_r2, [deliveries].[updated_at] AS t1_r3, [deliveries].[dont_select_me] AS t1_r4, [deliveries].[region_id] AS t1_r5, [deliveries].[package_id] AS t1_r6 FROM [packages] LEFT OUTER JOIN [deliveries] ON deliveries.package_id = packages.id WHERE (deliveries.name = delivery) AND [packages].id IN (1) dont_select_me is included. The docs say nothing of about :select being stepped on by fall back left joins (not the :include!). This seems like a bug. --~--~-~--~~~---~--~~ 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: Eager Loading a Relationship That Has No PK/FK
On Oct 8, 4:25 pm, Frederick Cheung frederick.che...@gmail.com wrote: On Oct 9, 12:01 am, MaggotChild hsomob1...@yahoo.com wrote: A Segment is linked to Media by Media.name, which is the result of concatenating Segment.name and Segment.part. As I said there are is no PK/PK relation so the actual Segment class looks like this: class Segment AR::Base def media @media ||= Media.find_by_name(#{name}%02d % part) end end This leaves me stuck with a N+1 select problem because I can't say: Segment.all :include = :media I think you'll find it hard to get that exactly to work. It shouldn't be hard however to do shows = Show.all :include = :segment load_media(shows.collect {|s| s.segment}) your load_media function will need to iterate over the segments, construct all the names, load those segments and then assign to each segment what its media is Hi Fred, The problem with that is the location of load_media(). Load media needs to be inside of Show, as it's responsible for loading a Show and it's Segments. Anyone client using a Show finder would now need the load_media() function. Plus load_media() is really doing what AR's association_preload is already doing. I figured out that there is actually an easy solution for this (too bad I'm using the CPK module which fails to support it -omitted for brevity): class Segment AR::Base belongs_to :show has_one :media, :primary_key = :media_name def media_name #{name}%02d % part end end Thanks! --~--~-~--~~~---~--~~ 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: Eager Loading a Relationship That Has No PK/FK
On Oct 9, 10:24 am, Marnen Laibow-Koser rails-mailing-l...@andreas- s.net wrote: MaggotChild wrote: I'm attempting to wrestle an old DB into Rails. This relationship is giving me trouble: class Show AR::Base has_many :segments end class Segment AR::Base belongs_to :show has_one :media #this has no PK/FK relation end A Segment is linked to Media by Media.name, which is the result of concatenating Segment.name and Segment.part. As I said there are is no PK/PK relation so the actual Segment class looks like this: class Segment AR::Base def media @media ||= Media.find_by_name(#{name}%02d % part) end end This leaves me stuck with a N+1 select problem because I can't say: Segment.all :include = :media The easiest thing to do would be to modify the media table so as to declare the name to be primary key. Hi Maren, Modifying the DB isn't possible but I could use the :primary_key options to has one: class Segment AR::Base belongs_to :show has_one :media, :primary_key = :media_name def media_name #{name}%02d % part end end Thanks! --~--~-~--~~~---~--~~ 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] Eager Loading a Relationship That Has No PK/FK
I'm attempting to wrestle an old DB into Rails. This relationship is giving me trouble: class Show AR::Base has_many :segments end class Segment AR::Base belongs_to :show has_one :media #this has no PK/FK relation end A Segment is linked to Media by Media.name, which is the result of concatenating Segment.name and Segment.part. As I said there are is no PK/PK relation so the actual Segment class looks like this: class Segment AR::Base def media @media ||= Media.find_by_name(#{name}%02d % part) end end This leaves me stuck with a N+1 select problem because I can't say: Segment.all :include = :media or Show.all :include = {:segment=:media} How to get around the N+1? select problem and eager load Media? In the case of Show.all, Show and Media have a relation (yes bad schema) so I was thinking I could pull all the Media after a the Segment collection is loaded by Show and assign them to the Segments accordingly. But, while there is an after_find, this is applied after each object and not after the collection. Any ideas? Thanks --~--~-~--~~~---~--~~ 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] Running Unit Tests With SphinxSE
Several of my unit tests run queries against SphinxSE. Since Rails creates the test DB by dumping the dev DB schema, my SphinxSE queries aren't run against the test DB. There seems to be no way to hook into the test:clone_structure task, so I thought that I'll just have the appropriate unit tests include this: module SphinxTest @@index = 'ct_test_index' mattr_accessor :index, :host, :port self class def included(klass) alter_test_table unless end #MySQL specific def alter_test_table return unless ActiveRecord::Base.connection.table_exists? (Query::TABLE_SPHINX) create = ActiveRecord::Base.connection.select_value show create table #{Query::TABLE_SPHINX} if @@index md = create.match %r|sphinx://([-.\w]+):(\d+)?/[- \w]+| host = @@host || md[0] port = @@port || md[1] alter = %|alter table #{Query::TABLE_SPHINX} connection=sphinx://#{host}:#{port}/#{@@index}| ActiveRecord::Base.connection.execute alter end end end end does anyone know of way to do this from outside my unit tests? On a side note, assuming portability was an issue, there seems to be know way to get the TableDefinition from the connection. Is this really the case? Thanks --~--~-~--~~~---~--~~ 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: Overriding AR read/write_attribute - Overridden write_attribute Is Never Called
On Sep 24, 12:34 am, Frederick Cheung frederick.che...@gmail.com wrote: On Sep 23, 11:27 pm, MaggotChild hsomob1...@yahoo.com wrote: Could someone explain this? At a quick glance it is probably because after AttributeMethods is included in ActiveRecord::Base, write_attribute is aliased overwridden (eg the change tracking module). You then change write_attribute on AttributeMethods but it is too late - the aliasing that occured in Dirty is pointing at the previous implementation. When you alias a method ruby does keep track of what the aliased method was at the time alias_method was called, for example: class Foo def to_be_aliased implementation 1 end alias_method :old_implementation, :to_be_aliased end class Foo def to_be_aliased implementation 2 end end Foo.new.old_implementation #= implementation 1 Hi Fred, Thanks for your response. I think you're on to something and I'll have to take a look a the Dirty module (amongst others). But, even if the module was aliasing the original write_attribute, I don't see how this would interfere with my redefinition. write_attribute is at the top of the call stack so I'd think it's going to use the most recent definition. Unless some other module redefined write_attribute within the scope of active AR::Base (as apposed to including it via a module) -which is possible. Using your example, I believe the case is more like the following: class Foo def to_be_aliased p implementation 1 end alias_method :old_implementation, :to_be_aliased end class Foo def to_be_aliased p implementation 2 end end Foo.new.to_be_aliased #implementation 2 --~--~-~--~~~---~--~~ 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: Overriding AR read/write_attribute - Overridden write_attribute Is Never Called
On Sep 24, 5:40 pm, MaggotChild hsomob1...@yahoo.com wrote: write_attribute is at the top of the call stack so I'd think it's going to use the most recent definition. Unless some other module redefined write_attribute within the scope of active AR::Base (as apposed to including it via a module) -which is possible. Yes, this is what happens, the alias is performed in a class_eval block giving it presidency over my attempted override in the module. --~--~-~--~~~---~--~~ 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] Overriding AR read/write_attribute - Overridden write_attribute Is Never Called
Could someone explain this? #config/initializers/ar_attributes.rb module ActiveRecord module AttributeMethods alias_method :ar_read_attribute, :read_attribute def read_attribute(attr_name) p read_override ar_read_attribute(attr_name) end alias_method :ar_write_attribute, :write_attribute def write_attribute(attr_name, value) raise 'You made it!' end end end In the Rails console: person.read_attribute :name read_override = Joe person.write_attribute :name, Bilal = Bilal person.read_attribute :name read_override = Bilal --~--~-~--~~~---~--~~ 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: App Setting, Where To Put: environment.rb, initializer.rb, lib..?
Thanks! --~--~-~--~~~---~--~~ 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] App Setting, Where To Put: environment.rb, initializer.rb, lib..?
I have a path that my model (Audio) uses to r/w files. This path differs between production, test, and dev. Audio includes a module that overrides some ActiveRecord methods. This module is contained in config/initializers. I'd like to set the Audio directory based on my environment, but using the given environment files fail because the module included by Audio hasn't been loaded. Of course I could just require it, but this made me think that maybe the initilaizers directory isn't the right place to put my AR overrides, and the environment file isn't the right place to put my non-Rails related environment settings. Is this the case? It seems as though I should set the directory in it's own initializer based on RAILS_ENV... but Rails is already doing this for me with the given environment files --~--~-~--~~~---~--~~ 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] Routes With Same URL Mapped To Different Actions
Can't find the answer to this. My old message received no responses (subject was messed up). Is it possible to create routes that have the same URL yet map to a different action based on the HTTP method (or pseudo method) like a Rails' resource? For example: map.resources :foo, :collection = { :purge = :get, :purge_them = :post } I'd like to use /foo/purge for both actions, and have the appropriate controller method called based on the HTTP request's method -like /foo does for the create and index methods, amongst others. --~--~-~--~~~---~--~~ 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] Custom Routes: Determining Action Named Based On Method
Is it possible to create routes that have the same URL yet map to a different action based on the HTTP method (or pseudo method)? For example: map.resources :foo, :collection = { :purge = :get, :purge_them = :post } I'd like to use /foo/purge for both actions, and have the appropriate controller method called based on the HTTP request's method -like /foo does for create and index, amongst others. --~--~-~--~~~---~--~~ 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] HTTP Auth 401 Even on Valid Login
I can't understand why authenticate_or_request_with_http_basic keeps failing even though I am entering the correct info. If I try to access /admin/items/new, I enter the correct info but I keep getting prompted. The strange part is that when I cancel out, I get HTTP Basic: Acce. Here's the controllers: class Admin::BaseController ApplicationController before_filter :authenticate def authenticate authenticate_or_request_with_http_basic do |name, pass| name == 'h' pass == 'x' end end end class Admin::ItemsController Admin::BaseController def new @item = Item.new @category = 'New Item' @categories = Category.find :all, :order='name' end #etc... end The log: Processing ItemsController#new (for localhost at 2009-05-03 12:20:05) [GET] Session ID: BAh7CDoSYXV0aGVudGljYXRlZFQ6DGNzcmZfaWQiJTYxZjNiYjBlY2Q2MWYz MTA3ZmQ0Y2ZiOTFhMmRlMWQxIgpmbGFzaElDOidBY3Rpb25Db250cm9sbGVy OjpGbGFzaDo6Rmxhc2hIYXNoewAGOgpAdXNlZHsA-- f03480a81df51ccf0c5d4a2a6e80f3aad10366cc Parameters: {action=new, controller=admin/items} Filter chain halted as [:authenticate] rendered_or_redirected. Completed in 0.00136 (733 reqs/sec) | Rendering: 0.00112 (82%) | DB: 0.0 (0%) | 401 Unauthorized [http://localhost/admin/items/new] --~--~-~--~~~---~--~~ 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] Overriding a Relationships Propertry
I have a child relationship that is denormalized. I want to normalize it at the object level once it's retrieved: class Parent has_many :denormalized_children, :class_name='Child' def children if @normalized_children.nil? @normalized_children = [] denormalized_children.group_by(:number).each { |c| @normalized_children NormalizedChild.new(c) } end @normalized_children end end This works fine, but I don't want to eager load them as :denormalized_children. I'd like to use :children. So I tried: class Parent has_many :children alias :denormalized_children :children def children #Same definition end end AR doesn't like this as it expects the children prop to return something that responds to loaded. has_many :children, :group='number' doesnt work either since it discards all but N unique numbers. So if I have 10 with number 1 and 5 with number 2 I'll only get back 2 children. Is there any way to do this without having to change the name of the relationship? I can't use after_load (or whatever it's called) in the Child class since the children need to be grouped by their relationship to their parent. Any suggestions would be great. Thanks --~--~-~--~~~---~--~~ 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] Creating/Updating Models from text/xml Requests
I'm curious how others would deal with this situation. In my case, I have: class Package has_many :deliveries end class Delivery belongs_to :package belongs_to :region end class Region has_many :deliveries end And accept XML requests in the following manner: package nameGood Shipmentname !-- snip other fields -- deliveries delivery statusFailure/status region nameUS/name /region /delivery delivery statusUnknown/status region nameFR/name /region /delivery /deliveries /package Now, my controller would look something like this ugly beast: class PackagesController def create if package = params[:package] package = Package.find_or_initialize_by_name(package) if deliveries = package.delete(:deliveries) deliveries.each do |d| region = d.delete(:region) region = Region.find_or_create_by_name(region[:name]) break if region.nil? package.deliveries.create(region) package.deliveries[-1].region = region end end end #save and more... end end As one can see, this is horrible for several reasons. One of the big ones being request validation and error handling. Overriding package.from_xml is an option, but I find myself thinking that I should be validating against request.body with say a DTD, as opposed to picking apart the params hash. (Though I'll have to pick it apart anyways to create the models...) Then I find my self thinking that creating a course grained validation method like a DTD is stupid since I have my fine grain validation logic within the given models. Yes, I've heard of ROXML. Any thoughts? --~--~-~--~~~---~--~~ 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: Multiple foreign keys create / destroy
class House ActiveRecord::Base belongs_to :country belongs_to :cities belongs_to :streets end Now...if i want to create a house like this: @street = Street.find(1) @house = @street.houses.new @house.name = Burlington house @house.save Now...only the street_id key is set in the house record...But not city_id and country_id... Is there an ORM way to let active record do the job ? That is the ORM way. The problem lies in your design (i.e. associations). A country has many cities. Cities have many streets. These streets contain houses. Sure a house belongs to a city, but given the additional entities involved the above model makes more sense. @house = some_city.streets.find(1).houses.create(:name='Adelitas') --~--~-~--~~~---~--~~ 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] Collection of Partials, :as Option No Good, TemplateError
I'm trying to render a collection of partials using :as. It raises an undefined local variable exception. Not sure why: ~/delivery-log/app/views/packages $ ls -1 _package.html.erb _package_list.html.erb index.html.erb index.html.erb: %= render :partial='package_list', :collection=@packages, :as=:package % _package_list.html.erb: tr td%= package.upc %/td /tr Seems simple, but it only raises the aforementioned exception: ActionView::TemplateError (undefined local variable or method `package' for #ActionView::Base:0xb7589ac8) on line #2 of packages/ _package_list.html.erb: 1: tr 2: td%= package.upc %/td 3: /tr --~--~-~--~~~---~--~~ 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: Collection of Partials, :as Option No Good, TemplateError
On Feb 24, 11:54 am, Eric ericgh...@gmail.com wrote: On Feb 24, 11:13 am, MaggotChild hsomob1...@yahoo.com wrote: I'm trying to render a collection of partials using :as. It raises an undefined local variable exception. Not sure why: Why not use :locals = ? Sure there a workarounds, but they don't explain why :as won't work. --~--~-~--~~~---~--~~ 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: ActiveRecord Unexplainable SystemStackError - Only in WEBrick
On Feb 3, 2:18 pm, Frederick Cheung frederick.che...@gmail.com wrote: On Feb 3, 7:33 pm, MaggotChild hsomob1...@yahoo.com wrote: On Feb 2, 2:54 pm, Frederick Cheung frederick.che...@gmail.com Does it only happen when class caches is false ? Why yes Fredrick, it does. That pretty much means that the automated reloading is bollocksed. OK, I understand this require_dependency thing but in my case I don't see how this applies. I thought that this code, in my lib dir, might be the cause: require 'activerecord' class Validator ActiveRecord::Base def self.columns @columns ||= []; end #snip ... end require 'validator' class SearchCriteria Validator #snip attribs end But, the source of my pain, this call: Package.search(criteria) executes: SHOW FIELDS FROM `packages` Delivery Load (0.000409) SELECT `deliveries`.* FROM `deliveries` WHERE (`deliveries`.package_id IN (319089123)) Delivery Columns (0.000961) SHOW FIELDS FROM `deliveries` Region Load (0.000370) SELECT * FROM `regions` WHERE (`regions`.id IN ('4803144','5750247')) when the Delivery class is being assembled by ActiveRecord - SystemStackError This has nothing to do with the above classes and or require_dependency... I think. class Package ActiveRecord::Base has_many :deliveries, #,:select='id, user, host, delivered, region_id, package_id', :include=:region, :dependent=:delete_all def self.search(criteria) raise ArgumentError, 'argument must be of type SearchCriteria' unless criteria.is_a?(SearchCriteria) clause = build_search_where_clause(criteria) find(:all, :include=[:deliveries], :conditions=clause) end def self.build_search_where_clause(criteria) where = [] [:region,:date,:status].each do |prop| case prop when :region where send(:sanitize_sql, [ 'deliveries.region_id in (?)', value ]) #snip clauses end where.join(' and ') end class Delivery ActiveRecord::Base belongs_to :package belongs_to :region end class Region ActiveRecord::Base has_many :deliveries end I'm at a loss -still. Thanks for your help. --~--~-~--~~~---~--~~ 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: ActiveRecord Unexplainable SystemStackError - Only in WEBrick
On Feb 3, 7:20 pm, Alan Brown stre...@gmail.com wrote: So how is the conditions clause built. can you log it before passing it to find? Yes I can see the clause in the development log. The query is valid, it executes fine. It's when the Delivery class is assembled by ActiveRecord that the stack exception occurs, Well, only when the action is processed by WEBrick. --~--~-~--~~~---~--~~ 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: ActiveRecord Unexplainable SystemStackError - Only in WEBrick
On Feb 2, 2:54 pm, Frederick Cheung frederick.che...@gmail.com wrote: On Feb 2, 1:33 pm, Ryan Bigg radarliste...@gmail.com wrote: Why not use a search plugin like thinking sphinx to do your searching for you? Overkill. The searching is rudimentary so I just quickly created something. In any case, I take it you had no thoughts as to why I'm receiving the stack error -other than possibly implying it was because of the SearchCriteria class? Does it only happen when class caches is false ? Why yes Fredrick, it does. I wonder why that can be. Your thoughts would be appreciated. Regardless, you have provided me with a new area to look into. Thanks! --~--~-~--~~~---~--~~ 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] ActiveRecord Unexplainable SystemStackError - Only in WEBrick
This SystemStackError is driving me crazy. It only takes place when testing with WEBrick - I can't reproduce it with any tests. It is caused by calling the missing id method (base.rb:2435) on my ActiveRecord class. WEBrick and my tests are running as the same user, with a `ulimit -s` of 10240. Plus the query works sometimes so I don't think this matters. Here's the trace: /usr/lib/ruby/1.8/set.rb:93:in `empty?' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ attribute_methods.rb:64:in `generated_methods?' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ attribute_methods.rb:237:in `method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ attribute_methods.rb:245:in `method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ base.rb:2435:in `hash' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ association_preload.rb:13:in `uniq' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ association_preload.rb:13:in `preload_associations' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ base.rb:1343:in `find_every' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ base.rb:536:in `find' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ association_preload.rb:254:in `find_associated_records' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ association_preload.rb:154:in `preload_has_many_association' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ association_preload.rb:40:in `send' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ association_preload.rb:40:in `preload_one_association' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ association_preload.rb:38:in `each' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ association_preload.rb:38:in `preload_one_association' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ association_preload.rb:17:in `preload_associations' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ association_preload.rb:16:in `preload_associations' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ association_preload.rb:16:in `each' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ association_preload.rb:16:in `preload_associations' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ base.rb:1343:in `find_every' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ base.rb:536:in `find' app/models/package.rb:29:in `search' The thing is, sometimes it works -at first. And usually results in the trace ending before Set.empty? at: /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/ attribute_methods.rb:64:in Here's the relevant parts of my code: class Package ActiveRecord::Base has_many :deliveries, #,:select='id, user, host, delivered, region_id, package_id', :include=:region, :dependent=:delete_all def self.search(criteria) raise ArgumentError, 'argument must be of type SearchCriteria' unless criteria.is_a\ ?(SearchCriteria) clause = build_search_where_clause(criteria) find(:all, :include=[:deliveries], :conditions=clause) end end class Delivery ActiveRecord::Base belongs_to :package belongs_to :region end class Region ActiveRecord::Base has_many :deliveries end Looking at the SQL output, the exception is occurring after the :deliveries relation is queried. At first I though it was because I left out the id field from the has_many :deliveries, :select='...' But I commented it out and still no luck. Any suggestions/ideas are greatly appreciated. --~--~-~--~~~---~--~~ 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: ActiveRecord Unexplainable SystemStackError - Only in WEBrick
On Feb 2, 1:33 pm, Ryan Bigg radarliste...@gmail.com wrote: Why not use a search plugin like thinking sphinx to do your searching for you? Overkill. The searching is rudimentary so I just quickly created something. In any case, I take it you had no thoughts as to why I'm receiving the stack error -other than possibly implying it was because of the SearchCriteria class? --~--~-~--~~~---~--~~ 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] Route Collections and HTTP methods
I don't see a way to create a single URL in a collection and then have routing dispatch to the appropriate method based on the HTTP method. map.resources :packages, :collection={ :search=:get } Gets me half way there, but I can see a way to say that I want the POST to go to the do_search method. Furthermore map.resources :packages, :collection= { :search=:get, :do_search=:post } results in a ActionController::MethodNotAllowed for POSTs to /packages/ do_search. `rake routes` affirms that this should be accepted: do_search_packages POST /packages/do_search {:action=do_search, :controller=packages} How can I solve either of these? Thanks --~--~-~--~~~---~--~~ 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: Route Collections and HTTP methods
..but in general searches are GET and never POST. I don't know where this nonsense comes from but it's breaking the principles behind the HTTP. Well I don't disagree with this paradigm, but the point is Rails seems to not allow one to set up a route like the defaults provided by map.resourses. i.e one URL, dispatched to to 2 different actions based on HTTP method. Did you try :any instead of :post ? Either one raises the nonsensical ActionController::MethodNotAllowed. I say nonsensical because rake routes tells me do_search accepts posts. --~--~-~--~~~---~--~~ 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] url_for, routes, and the current url
I need to generate 2 types of URL and have defined my routes as: map.country_yes /:country/ yes, :controller=posts, :action=country, :yes=true map.country_no /:country/ no, :controller=posts, :action=country, :yes=false map.yes /yes, :controller=posts, :action=index, :yes=true map.no /no, :controller=posts, :action=index, :yes=false The problem is that, when a request if for /yes or /no, url_for() doesn't generate URLs for the country. url_for(:yes=true) generates the correct url no matter where its used. if the current request is for /no it generates /yes. if its for /mexico/no, it generates /mexico/yes though this is not the same when trying to generate URLs for country. If the request is at /no url_for(:country='usa',:yes=true) generates /no and tacks on 'usa' and 'yes' as query string parameters instead of generating /usa/yes. Why is this taking place? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---