[Rails] Re: Using mysql's 'REPLACE' command in active record

2009-03-18 Thread Mark Reginald James

Richard wrote:
> Is this possible ??

Yes, but only with custom SQL:

Model.connection.insert "replace into #{Model.table_name} "

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: set_table_name woes, fragmented domain class

2009-03-18 Thread Mark Reginald James

Michael J. I. Jackson wrote:

> I'm running Rails on top of a Mongrel cluster. I have several model  
> classes that are going to have so many records it is not feasible to  
> keep them all in the same table. So I've split out the records into  
> many different tables, each with its own suffix which corresponds to  
> the id of the model to which all records in that particular table  
> belong.
> 
> For example, if I have a Domain AR object with an id of 20, all  
> DomainURL objects that :belong_to that particular Domain object are  
> stored in table domain_urls_20.
> 
> This necessitates a call to set_table_name before the DomainURL  
> objects are fetched, like so:
> 
> DomainURL.set_table_name "domain_urls_" + domain.id
> 
> However, when I go into production mode, I get mismatched table and  
> column names on my find's. I am assuming this is caused by  
> ActiveRecord caching the domain model so that set_table_name is  
> rendered ineffective when it goes to fetch a record. However, I'm not  
> quite sure if this is the case.

Perhaps your problem is caching of table names by the urls
association. You could clear the cache using something like

class Domain
   has_many :urls, :class_name => 'DomainURL'

   def set_url_table
 self.class.reflect_on_association(:urls).instance_eval do
   @table_name = @quoted_table_name = nil
 end
 DomainURL.set_table_name "domain_urls_" + id
   end
end

domain = Domain.find(...)
domain.set_url_table
urls = domain.urls

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: config.cache_classes breaks reflect_on_assocation ?

2009-02-19 Thread Mark Reginald James

dino d. wrote:

> def self.set_current_user user
> if user
>   holder = user.id
> else
>   holder = -1
> end
> reflect_on_association(:current_user_vote).options[:conditions] =
>   "votes.user_id = #{holder}"
> self.current_user = user
>   end
> 
> This works fine, unless I enable config.cache_classes, then, the first
> user who accesses the site after the server is restarted gets his id
> set to current_user, and everyone else afterward gets the first guy's
> user_id set in the association.

Yes, this trick stopped working in 2.2 because the reflection
now caches its conditions SQL.

One workaround is

reflection = reflect_on_association(:current_user_vote)
reflection.options[:conditions] = "votes.user_id = #{holder}"
reflection.instance_eval { @sanitized_conditions = nil }

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: conditions on association include, hacky but more or less solved

2009-02-19 Thread Mark Reginald James

Chris Warren wrote:

> How do you dynamically set the association conditions? I've done a fair 
> amount of programming, but am new to RoR - I abandoned that approach 
> because I thought the conditions were fixed when the object was defined.

http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/16e51993be4f52f6

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Associated child records not created on creation of a parent.

2009-02-19 Thread Mark Reginald James

Master Chief wrote:
> I am trying to create a record with association.  Within the create
> method, I would also like to pre-populate dependent (has_many) records
> from a template in the database.

I'm not exactly clear what you're doing, but here are some
comments that may help.

>1. class PrototypeModel < ActiveRecord::Base
>2.   belongs_to :author, :class_name => 'User'
>3.   has_many :departments
>4.   belongs_to :prototype_model
>5.   acts_as_tree :foreign_key => "parent_id"
>6. end
> 
>1. class Department < ActiveRecord::Base
>2.   validates_presence_of :name, :description
>3.   validates_presence_of :parent_id
>4.   acts_as_tree :foreign_key => "parent_id"
>5.   belongs_to :department
>6.   belongs_to :prototype_model
>7. end

1. Are the two belongs_to associations that reference their
own class necessary, or can you use the parent association
generated by acts_as_tree?

2. You should use "validates_presence_of :parent" instead.
Using the foreign key will prevent validation when the
parent is new.  You have to make sure that either
(1), the foreign-key is non-NULL and points to an existing
record, or (2), that a new parent object is assigned to the
association.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Execute an SQL statement each time DB connection is set up

2009-02-19 Thread Mark Reginald James

Alex Fortuna wrote:

> I need to execute a "SET SQL_MODE='STRICT_ALL_TABLES'" after my
> application's DB connection is established by AcriveRecord. This is
> important since I want the DB itself be more strict about logical data
> integrity.
> 
> Which is the proper place within application code from where I can issue
> this query?
> 
> 
> Tried to make an ActiveRecord::Base.connection.execute() in an
> initializer, but it only runs once after server startup. At next request
> the connection is re-created, but initializers aren't invoked.

In 2.2.2 there doesn't seem to be a callback when a connection
is established & re-established, so try an initializer like

ActiveRecord::ConnectionAdapters::MysqlAdaptor.class_eval do
   private
   def connect_with_strict
 connect_without_strict
 execute "SET SQL_MODE='STRICT_ALL_TABLES'"
   end
   alias_method_chain :connect, :strict
end


-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: conditions on association include, hacky but more or less solved

2009-02-19 Thread Mark Reginald James

Chris wrote:

> I just finally figured out how to get 2.2.2 to do this, and thought
> I'd share in case others run into the same thing.
> ...
>  :joins => " AND
> key_factors.entity_id="+...@entity.id.to_s,
> 
> This find specification gets the job done, but it's a pretty nasty
> hack. It won't work if you need this kind of trick on more than the
> last association, and it won't work if the SQL builder changes.
> Ideally You'd be able just to specify the entire join clause
> in :joins, or else specify it for each :include-ed thing, or else have
> parametrized associations in the model.

That's a useful trick to know.

An alternative I've been using is to dynamically change the
:conditions option of association reflections. The advantage
is that it works for all includes rather than just the last include,
but the disadvantage is that it's not thread safe, unless you
put a lock around the query.

I'd like to see explicit support for dynamic conditions,
by storing any dynamic change in thread variables.

> I think a fully specified :joins is probably the proper way to do
> this. Currently having both the includes and the joins causes a "Not
> unique table/alias" error. I suspect that's a bug / oversight in the
> SQL generator. It does a good job of figuring out that JOINs are
> needed when a table is mentioned in :conditions, but it skips the step
> of checking to see if that join is already specified in :joins. Even
> better, since the :conditions are only there to get the associations
> to join in a single query, the builder could check :joins as well
> as :conditions to see if it should switch to that method (from the
> multi-query method), then the dummy conditions could be left out
> entirely.

Agree. Or simply an option that forces single-query mode.


-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Redirect_to Vs Render_component

2009-02-14 Thread Mark Reginald James

Srividya Sharma wrote:
> Hello
> I am using Rails 2.2.2.
> Here is my problem:
> I have a controller action which will receive several parameters,
> validate them, process them and create a new set of parameters out of
> them.
> Then, this controller should delegate the task to another controller
> action based on some values and pass these processed parameters to it. I
> have stored the processed parameters in a hash. (there are many of them)
> 
> I could only see two options to achieve this:
> 1. Use redirect_to and pass each parameter as a string.
> 2. Use render_component and pass the parameters as a hash.
> 
> I have seen that render_component is not recommended in most cases. Is
> it recommended solution for this case? I think render_component slows
> down the request also.
> Which option is right for me?
> Are there any other ways to achieve this? Please guide me.

If you don't want the browser url to change, you can just call
the other action like a method, passing the parameters in either
an instance variable or a method parameter (that's default nil
to allow it to be called externally). Or are you messing with
params directly?

This is assuming the other method is in the same controller.
If it isn't, you'll have to make the other method available
within the first controller, either through inheritance or
mix-ins.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: How can I retrieve the SQL during each model action?

2009-02-14 Thread Mark Reginald James

xeon wrote:
> Hi,
> 
> I would like to know for each running model action, how can I retrieve
> the SQL running behind? I don't want to scan log each time for the sql
> running behind.
> E.g. post.find(:all,:conditions=>{:id=>'1'}
> 
> I wanna debug the sql generated behind, is that other shorthand
> function like post.find().show_sql() that enable
> us to view what's SQL generated ?

That sounds like a good idea. Perhaps a
:log => :instance_variable_name option to find.

One alternative is the query_trace plugin that displays
a backtrace for each query, so that the log shows what
line of code is associated with each query.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: page.replace_html and

2009-02-13 Thread Mark Reginald James

Raimon Fs wrote:
> Hello,
> 
> I have a page with a form and a div area that I update after submitting
> the form.
> 
> I want to replace the Div area with new information.
> 
> In the page, inside the div area, I have:
> 
> 
>   <%= render :partial => "list" %>
> 
> 
> as I want to show the current records the first time the page opens.
> 
> The problem is after submitting, I the 'old values' of the Div remain in
> the Div, and now I have the new values with the old values. If I
> continue to add some new values, I get always on top the new ones and
> after the 'old ones', I get TWO tables, the new one and the old one.
> 
> How I can remove the old table 
> 
> If for example I add some text on the top of the _list (partial included
> in the Div), this text gets removed but NOT the table.
> 
> Any ideas ?
> 
> I'm updating the div with a .js.rjs file:
> page.replace_html("div_listd", :partial => "list")
> 
> 
> If in the page I don't put the   <%= render :partial => "list" %>, I
> don't see the inital records (correct), and subsequent forms submitted
> show the resulted records ok, so the .js can replace the content of the
> Div area ...

Are you sure that your HTML is correctly nested?

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: How to show error messages in pop up or confirm screen ?

2009-02-13 Thread Mark Reginald James

Adriano Lul wrote:
> I would like to show messages when the validation fails (must be number,
> cant be blank, etc)  but not in the main page I would like to open a pop
> up or better yet put it in a confirm screen where user would read and
> thereafter click ok.

render :update do |page|
   if obj.save
 ...
   else
 page.alert obj.errors.full_messages.join("\n")
   end
end

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Transaction block still sets id on model/new_record to false on rollback

2009-02-13 Thread Mark Reginald James

Farrel wrote:
> I'm on Rails 2.2.2 on MySQL with InnoDB table types. I'm running into
> a wierd bug where if I have two models in a transaction and the second
> model raises an exception, the first model still has it's ID set and
> it's new_record status set to false despite the row the ID is set to
> never existing.

Yes, that's the way Transactions (now) work.

The correct way to handle this is to not proceed with
a transaction unless all model instances being saved
in that transaction have been validated.

The alternative is to wrap rollback_active_record_state!
blocks around the transaction for all instances being
saved except the last.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: another collection_select, HABTM, :multiple question

2009-02-13 Thread Mark Reginald James

Sergio Ruiz wrote:

> ,[ worker ]
> | class Worker < ActiveRecord::Base
> |   has_and_belongs_to_many  :skillsets
> |   belongs_to :instructor
> | end
> ...
> | <%= collection_select(:worker, :skillsets, Skillset.find(:all), :id, 
> :title, {}, {:multiple => true}) %>
> ...
> + when the select box is rendered, it it shows all the skillsets
>   correctly, but, the correct ones, the ones that are part of the
>   Worker model are not selected. nothing is selected.

Try

collection_select(:worker, :skillset_ids, ...

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: eager loading kills the :select clause in ActiveRecord

2009-01-30 Thread Mark Reginald James

dino d. wrote:

> I've dramatically improved my SQL performance with eager loading, but
> the problem is, it kills the :select directive because it generates
> its own (it ONLY selects the table fields  and eager loaded table
> fields).  Is there a way around this?  I have some left outer joins in
> my :joins clause, and some aliasing in my :select clause, but
> the :include clause kills the select aliases.  any advice?

Not supported at present, though I have a plugin that makes
this work for Rails 2.0.2, not yet ported to 2.1+:

   http://dev.rubyonrails.org/ticket/7147#comment:12

There is another plugin that works with 2.2 that allows you
to load object hierarchies when using custom SQL:

   http://rubyforge.org/projects/eagerfindersql

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Rails.root problem

2009-01-30 Thread Mark Reginald James

Dan Smith wrote:

> I have a form which lets users upload an flv file to the database, this
> should then be played by the "player.swf" file in the public directory.

src="/player.swf?..." should work

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Rails runtime

2009-01-28 Thread Mark Reginald James

Camille Roux wrote:

> I've a question about the Rails runtime.
> I'd like to know which part of Rails is executed (and put in memory) at
> each request? at each server launch?
> In other words, is Rails like PHP (the whole code is "executed" at each
> request)?

On on start-up in production mode a Rails app will load, execute,
and cache the Rails framework plus certain parts of the app code.
The rest of the app code is loaded and cached on-demand when an
unknown class or module is referenced during a request.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Plugin Module Help

2009-01-28 Thread Mark Reginald James

Nate Leavitt wrote:

> Is the thread specific for the request in rails? Meaning.. the 
> before_filter will be run on each rails action/request therefore is a 
> new thread created in rails for that process?  Jeez.. I hope I'm 
> explaining it properly :)

Yes, each thread only carries one request at time.

> Also, since that before filter is creating a new AppConn obj do I have 
> to worry about performance?  It just seems that creating a new AppConn 
> for each rails request seems like overkill.  Am I wrong in thinking 
> that?

Creating such a small AppConn object with each request shouldn't
be a problem.

If it is you can instead do

def self.set_account(url, key)
  if ac = Thread.current[:api_conn]
ac.url, ac.key = url, key
  else
Thread.current[:api_conn] = ApiConn.new(url, key)
  end
end

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Plugin Module Help

2009-01-27 Thread Mark Reginald James

Nate Leavitt wrote:

> Is that multi-thread safe for the newer versions of rails?

Yes.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: changing model type

2009-01-27 Thread Mark Reginald James

Jared Moody wrote:
> I have a form that allows a User to add/edit the additional Users or
> LoginUsers to/on their account, but the wrong validations are run when
> updating a User to be a Login user or vice-versa.
> ...
> # revoking login privledges
> if @user.is_a?(LoginUser) && !params[:allow_login]
>   #switch the type here, tried a few different things but none work
>   @user[:type] = "User"
 @user.save_without_validation!
 @user = User.find(@user.id)
> end

Try adding the above two lines.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: ajax search feature works in FF but not IE

2009-01-27 Thread Mark Reginald James

Pardee, Roy wrote:

> <%= observe_field('project_search',
> :url => { :controller => "projects", :action => "index_search"},
> :update => "project-list", 
> ...
> That partial renders a table w/one row per project.  This works just fine 
> from FireFox.  But from IE I don't get my table--just a blank page.  So the 
> records that used to be in my table id="project-list" are gone, but I don't 
> see the new ones that index_search queried out of the db.  Judging from the 
> log, IE is indeed making the request to index_search, passing the params it 
> should, etc.  The log even says, e.g.,
> ...
> But IE doesn't seem to want to actually *display* these guys.  FF's web 
> development toolbar tells me I'm in "standards compliance mode" when I pull 
> up a searched list of projects, so I believe the markup is all kosher.


:update changes the innerHTML of a node, so you will be inserting
the table inside the table tag.

Wrap a div around the table and update that instead.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: acts_as_enumerated

2009-01-26 Thread Mark Reginald James

Paul Nelligan wrote:

> I want to use this plugin for a many-many relationship with a join 
> table, with one of the models being enumerated... does it still hold the 
> same benefits with respect to caching??

Sure. All instances will have their attributes cached, but the
association will also be cached. So you'll have to reload an
association if you want the latest data.

> also, if the size of the table for the enumerated model is very large 
> (though still a defineable set), e.g. regions of each country of the 
> world, is it still advantageous to use this system?

Well, it's an easy and fast way if you think the memory usage is
worth it. But if it's taking too much memory for the benefit
gained, you may instead wish to use a limited-size LRU cache.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: habtm relation and dependent => :destroy

2009-01-26 Thread Mark Reginald James

Panda Beer wrote:
> Hi all ,
> I have a habtm relation between 2 tables:
>  - users
>  - rights
> 
> If there a way to affect a destroy dependance on those 2 tables only for
> the last instance? In my opinion, dependent => :destroy is not the
> solution because this delete all the linked instance.
> 
> 
> For example, if I have:
> # in User model this:
>   - "Joo"
>   - "Jea"
>   - "To"
> # in Right model this:
>   - "Write"
>   - "Read"
> # in the habtm relation this:
>   - "Joo" can "Write"
>   - "Joo" can "Read"
>   - "Jea" can "Read"
> 
> I would like to be able to delete "Read" in Right model, and automaticly
> deleting "Jea" user too, because this user have no other right. But
> without deleting "Joo" user (because he remains have a right named
> "Write").

Perhaps something like:

class Right
  after_destroy do
User.find(:all, :uniq => true, :joins => :rights,
  :conditions => 'rights.id is NULL').each(&:destroy)
  end
end

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Belonging models not saving individually

2009-01-26 Thread Mark Reginald James

Macario Ortega wrote:

> The problem is that the Photos don't get saved in the database although
> they return false for #new_record? and Paperclip saves the files, if I
> set a debugger here:
> 
>   def create
> @page = Page.new(params[:page])
> 
> if @page.save
>   ...
> else
>   debugger
>   render :action => "new"
> end
>   end
> 
> and play with the irb:
> 
>>> @planta.fotos.first
> => #>> @planta.fotos.first.new_record?
> => false
>>> Photo.count
> => 0
>>> Photo.find( 1 )
> => ActiveRecord::RecordNotFound Exception: Couldn't find Photo with ID=1
>>> @planta.fotos.first.save( false )
> => true
>>> Photo.count
> => 0
> 
> This is most annoying and strange, it should be saved!!!
> 
> 
> In the log it appears as it has ben written to db:
> Photo Create (0.8ms)   INSERT INTO "photos" ("updated_at"...
> 
> Is there any kind of transaction or caching involved? Can I override
> this behavior?

Rails automatically wraps saves in transactions, but does not
reset the ids of successful saves within a rolled-back save,
even though those saves will themselves be rolled-back.

You'll have to do something like

@page.save_without_transactions

or save your photos outside the page save.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Plugin Module Help

2009-01-26 Thread Mark Reginald James

Nate Leavitt wrote:
> First off, I'm an intermediate Rails developer who is still learning the
> inner workings of Rails.. so here is my situation/problem:
> 
> I have written a Plugin that accesses a third party API.  Within the
> Plugin I had multiple classes that inherit from one another.  Example:
> 
> **
> Old Code (Plugin Classes)
> **
> 
> class ApiConn
>  attr_accessor :api_url, :api_key
> 
>  def initialize(url, key)
>@api_url = url
>@api_key = key
>  end
> 
>  def api_perform(class_type, method, *args)
>begin
>  server = XMLRPC::Client.new3({'host' => self.api_url, 'path' =>
> "/api/xmlrpc", 'port' => 443, 'use_ssl' => true})
>  result = server.call("#{class_type}.#{method}", self.api_key,
> *args)
>rescue XMLRPC::FaultException => e
>  puts "*** Api Error: #{e.faultCode} - #{e.faultString} ***"
>end
> 
>return result
>  end
> end
> 
> class ContactService < ApiConn
>   def api_contact_add(data)
> api_perform('ContactService', 'add', data)
>   end
> end
> 
> 
> 
> **
> Then within my ApplicationController I have this:
> **
> 
> Account
> def current_account
>   @current_account ||=
> Rails.cache.fetch("ch:current:account:#{current_subdomain}"){
> Account.find_by_subdomain(current_subdomain)
>   }
> end
> 
> def app_contact_svc
>   @app_contact_svc ||=
> ContactService.new("#{current_account.api_url}.application.com",
> current_account.api_key)
> end
> 
> 
> **
> 
> 
> Well, this has worked fine.  However, as I've been going through my
> controllers, I've noticed that a lot of these calls should be moved to
> the models.  For example, I have an ActiveRecord model named Contact and
> I would like to be able to call api_contact_add from within the instance
> of that model.  I know I would be able to do that by switching the
> plugin to a module.
> 
> However, here is the problem:  The rails app uses accounts and each
> account has a different api_url and api_key.  The current_account(see
> above) is set by the subdomain being used.
> 
> So I guess my question is how I can have module attributes set
> specifically for each account through multiple models?
> 
> So instead of doing Contact.new(api_url, key) in an initialize statement
> for each class that includes the ApiConn module, I would like these
> values set once for all models specific to the account.
> 
> I hope this make sense :)

Perhaps something like:

module ContactService
   def self.set_account(url, key)
 Thread.current[:api_conn] = ApiConn.new(url, key)
   end

   def api_contact_add(data)
 Thread.current[:api_conn].api_perform('ContactService', 'add', data)
   end
end

before_filter do
   ContactService.set_account(
   "#{current_account.api_url}.application.com", current_account.api_key)
end


-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: how make attachment_fu with 2 has_attachment

2009-01-24 Thread Mark Reginald James

anton effendi wrote:
> Thank you..
> Your ide must use 3 model.. 1 model -> User, 1 model -> for admin, 1 
> model -> for user
> is true?
> But I want use 1 model for admin and user..
>  so if I call "user = User.create" in admin and in user, photo will 
> upload.. with different size thumbnail.
> 
> Can u explain with code?? thank you very much

class User < ActiveRecord::Base
   # Database table "users", having string field "type"
   # Put methods common to all users here
end

class OrdinaryUser < User
   has_attachment :thumbnails => ...
end

class Admin < User
   has_attachment :thumbnails => ...
end

user = OrdinaryUser.create!
admin = Admin.create!

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: find_by_sql error, any ideas please

2009-01-24 Thread Mark Reginald James

elioncho wrote:

> User.find_by_sql "update users set name='Test'"

Use User.update_all ['name = ?', 'Test']
or  User.update "update users set name='Test'"

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: getValue() in a Controller problem, help please.

2009-01-24 Thread Mark Reginald James

David Sousa wrote:
> Hello Mark, thanks... but, my problem is bigger than that or I don't 
> know how that the answer is easy.
> 
> So, I have a structure like this. (view)
> 
> 
> <% @client_list.each do |client| -%>
> <%= client.name%>
> ... .. .. .. ..
> <% end -%>
> <%=will_paginate(@client_list, :params => {:controller => "client", 
> :action => "search_result", :foo => "$F('name')"})%>
> 
> 
> 
> 
>   Nome
>   <%= text_field_tag :name%>
>     ...
> 
> <%= observe_form :search_form, :frequency => 0.5, :url => { :action 
> => "search_result" } %>
> 
> 
> 
> What I wanna do is, pass the :name.value of the search form to the 
> paginate action, so when the result is paginated I will still have the 
> same results. Otherwise, when I go the the second page for example, I 
> will have the second page of all my clients, not the result of the 
> search.

Since you're using observe_form, params[:name] will be set
in your controller action, which can be used in the view.
e.g. :foo => params[:name]

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Runner suddenly fails...

2009-01-23 Thread Mark Reginald James

Mattias Bud wrote:

> `real_connect': Unknown database 'dbname_production' (Mysql::Error)

Does this exist, or do you want to use the dbname_development database
by leaving the "-e production" option off the runner command?

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Recovering Mysql::Error - Must I redo establish_connecti

2009-01-23 Thread Mark Reginald James

Fritz Anderson wrote:

> The application is not full Rails; it has no ActiveController instances.

That'll mean you'll miss out on Rails automatic connection verification.

>> You might like to call Message.verify_active_connections!
>> before your find.
> 
> An interesting proposition. verify_active_connections! has no rdoc 
> available, and in fact abstract/connection_specification.rb marks it 
> :nodoc:. Doesn't this suggest it isn't for typical use?

True. But your situation is not typical.

> I don't exactly see what the method does when a connection is lost. Its 
> last construct is a Hash#each_value, which doesn't suggest a useful 
> return value. Does it raise an exception? Which? And what am I to do 
> with it once I rescue it?

You just call it (ignoring any return value) before your find
to ensure the model's DB connection is established.

> Also, while I'm on Rails 1.2.6, I see that the method is deprecated for 
> 2.2.1 (and still undocumented as of 2.1).

On later versions of AR you can instead do Message.connection.verify!

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: getValue() in a Controller problem, help please.

2009-01-23 Thread Mark Reginald James

David Sousa wrote:
> Hello there,
> 
> I'm trying to do something like:
> 
> render :update do |page|
> page[:client_list].replace_html render(:partial => 'client_list',
> :object => @clients_list)
> name = page[:name].getValue();
> end
> 
> but I'm not getting the value, or somehow use $("name").getValue()
> inside of the controller.

You will need to send this value as a parameter of your AJAX
call, using the :with parameter of the form helper.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: how make attachment_fu with 2 has_attachment

2009-01-23 Thread Mark Reginald James

anton effendi wrote:

> I want to make upload photo with attachment_fu.
> 
> this logic is
> if admin
> 
> has_attachment :content_type => :image,
>  :storage => :file_system,
>  :path_prefix => 'public/photos',
>  :processor => 'Rmagick',
>  :thumbnails => {:standard => "520x520>", *:thumb => 
> "110x110>"*, :medium => "250x250>"},
>   :size => 0.kilobytes..5000.kilobytes
> 
> 
> if user
> has_attachment :content_type => :image,
>  :storage => :file_system,
>  :path_prefix => 'public/photos',
>  :processor => 'Rmagick',
>  :thumbnails => {:standard => "520x520>", *:thumb => 
> "120x120>"*, :medium => "250x250>"},
>   :size => 0.kilobytes..5000.kilobytes
> 
> 
> I want to make it in 1 model... because I save to 1 table...

Put these in Admin and OrdinaryUser sub-classes/sub-models of your
main User model. If you add an STI "type" field to this model then
both these models will still use the users table if you properly
instantiate them.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: forms and controller scope

2009-01-23 Thread Mark Reginald James

Adam Akhtar wrote:
> Is it possible to pass predefined arguments along with a form with
> form_for? i.e. an id etc?
> 
> I have a task model with usual title, completed(bool), duedate and a
> foreign key field called project_id (a project has many tasks). On one
> view i want to have an "express add task" form i.e. there is only one
> field to fill in for the title. The rest of the attributes I want to set
> to some default values. The problem is this form will be sent to a
> dffierent controller.
> 
> As the user only submits a title ill also need the project_id which is
> known is available in the view...is there anyway i can pass this along
> with the form?

You can put some fixed parameters in the path part of the form's
action URL, which will be extracted by the Rails routes you define,
plus put some more parameters in hidden form fields.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Recovering Mysql::Error - Must I redo establish_connection?

2009-01-21 Thread Mark Reginald James

Fritz Anderson wrote:
> Periodically my application (a faceless daemon, so there's no
> human-interface considerations) gets the following exception:
> 
> Mysql::Error: MySQL server has gone away: SELECT * FROM `messages` WHERE
> (`messages`.`disposition` IS NULL)  ORDER BY senttime
> (ActiveRecord::StatementInvalid)
> 
> I've concluded this is an inevitable vicissitude of our MySQL server
> setup. I'm now catching the exception. My question is what I should do
> once I catch it.
> 
> I am sleeping for 5 minutes and retrying the find(). Is this enough?
> 
> Or should I re-run ActiveRecord::Base.establish_connection as well?

Is this in response to a normal controller request?

You might like to call Message.verify_active_connections!
before your find.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Runner suddenly fails...

2009-01-21 Thread Mark Reginald James

Mattias Bud wrote:
> I have some jobs running through cron jobs and runner that suddenly
> starts failing.
> 
> This is how it looks:
> 
> from
> /usr/local/lib/ruby/gems/1.8/gems/rails-2.1.2/lib/commands/runner.rb:47
>   from
> /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
> `gem_original_require'
>   from
> /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
> `require'
>   from /usr/local/www/letter_archive/crontabs/../script/runner:3
> 
> This only happens if the job gets triggered by cron not if I do it by
> hand.
> 
> I suspect this started hapening when I upgraded to gem 1.3.1
> 
> Any ideas?

I'd have to see more of the stack trace to be sure, but it's
probably due to problems with either the PATH or GEM_HOME
environment variables in the cron shell context.

Try a crontab command like

env PATH=/usr/local/bin:/usr/bin GEM_HOME=/usr/local/lib/ruby/gems/1.8
/usr/local/www/letter_archive/script/runner -e production 

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: PostgreSQL specific data types in migrations

2009-01-21 Thread Mark Reginald James

Michael Graff wrote:
> I am willing to give up the common denominator in database
> compatibility and dive into using some PostgreSQL specific types.  In
> particular, I need bigint (int8) id columns and 'cidr' data type.

Can't help you on the cidr, but to change the id to bigint I've used

change_column :probes, :id, :bigint, :limit => 8

after the create_table block.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: tricky issue - AJAX + form text fields + sortable_list

2009-01-21 Thread Mark Reginald James

Taylor Strait wrote:
> I have a custom form builder that has a question type of "rank order."
> That is where you rank the choices in order from 1-5, or whatever.  So I
> have the answer choices set up as a sortable list that has a text field
> with the order position.  Using AJAX, I can change the order of these
> answers AND update the text fields with the proper numerical rank.
> 
> The problem is that when the form is POST submitted, it does not use the
> AJAX-updated values in the text fields but the original values from the
> page load DESPITE the text fields being different.  If I manually change
> the field by typing, it POSTs correctly.  How can I force the form to
> grab the "fresh" values from the text fields on submittal?

What browser are you using, what code are you using to set the
field contents, and what version of Prototype are you using?

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: noob question about development mode

2009-01-21 Thread Mark Reginald James

Kyle Wakefield wrote:

> If not, does someone know what I could be missing in my Apache 
> configuration that is keeping me from accessing development mode?

Are you using Passenger (mod_rails)? If so, you need to add the
"RailsEnv development" option to your VirtualHost config.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Easy searching by hierarchical category...

2009-01-13 Thread Mark Reginald James

adamfowleruk wrote:
> I recently wanted to come up with a solution to searching by a
> category (E.g. Shoes), sub category (E.g. Shoes > Blue), or sub sub
> category to any depth. This represents an obvious DB challenge not to
> mention model objects in RoR. I've come up with an elegant solution
> using Binary Trees and thought it might be useful to others. I've
> documented it here:
> 
> http://web.me.com/adamfowleruk/adamslife/Blog/Entries/2009/1/13_A_Strategy_for_Hierarchical_Searches_in_a_single_SQL_entity_table.html
> 
> Note I've not used it in anger yet, and have not QA'ed the Ruby and
> Java code at the end of the entry, but the approach is simple enough
> and easy for anyone to pick up.
> 
> Category searching to any depth using 1 column/model variable. 8o)
> 
> I'd appreciate any feedback, especially if anyone can think of a more
> elegant way to do it?

Adam, the usual way this is done is with the nested-set data
structure, which effectively also uses btrees in the DB index
for the "left" column. Searching by pattern match would be
somewhat slower than such indexed nested-set searches.

Your method does have the advantage of being able to add children
without having to fix-up the tree afterwards, and for giving each
node a permanent hierarchy identifier that makes it easy to do
subcategory searches in search-engine indices. The nested-set
method can however be tweaked to have the same properties.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Quick validation question

2009-01-13 Thread Mark Reginald James

Dan Smith wrote:
> Hi, I have a model which contains a boolean value.  I would like to set
> a validation which only allows this value to be set to true for one
> record at a time in the table.  Is it possible to do this with
> validates_uniqueness?  or is there another way?

validates_uniqueness_of :is_root, :if => :is_root should work.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: dynamically generated CSV files

2009-01-09 Thread Mark Reginald James

Jason Pfeifer wrote:

> So if I have that correct, I don't understand how the send method 
> operates on the result object.  Is it just passing a method name?  I 
> guess I was just mistakenly thinking of it in terms of key value 
> pairing:
> 
> result.send(a) AS result[a]

result.send(a) just calls the method with name a on the result object.

result[a] would also be acceptable, accessing the attribute directly,
but failing if you've written a custom accessor method for that
attribute.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: dynamically generated CSV files

2009-01-09 Thread Mark Reginald James

Jason Pfeifer wrote:
> Greetings, I have a method from which I want to generate CSV files based
> on the model name:
> 
> def export_table_to_csv(table)
> 
>   require 'csv'
> 
>   @table = table
>   @results = @table.find(:all)
>   @titles = @table.column_names
> 
>   report = StringIO.new
>   CSV::Writer.generate(report, ',') do |title|
> title << @titles
> @results.each do |result|
>   title << result.attributes.values
> end
>   end
> 
>   report.rewind
>   file = File.open("#{RAILS_ROOT}/the_directory/#...@table}.csv", "wb")
>   file.write(report.read)
> 
> end
> 
> The above script works fine right now, however the call to
> result.attributes.values returns them in a different order than
> @table.column_names, so that the values end up in the wrong places. ie.
> the name will be under the email column etc.
> 
> any thoughts on how I can make it spit out in the order it's in from the
> database?

Replace title << result.attributes.values with

   title << @titles.map { |a| result.send(a) }

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Accessing request.host in UserMailer

2009-01-09 Thread Mark Reginald James

Tom Hoen wrote:
> Mark Reginald James wrote:
> 
>> You'll have to supply the request object as a parameter to
>> mailer calls made in your controllers.
> Mark, I appreciate the quick response.
> 
> Unfortunately, I am using an Observer to initiate the emails. So I don't 
> think I have access to the request object when the deliver call is made.
> 
> 
> Is there any other way to access it?

Tom, perhaps you can use the second method I described,
but make it thread-safe:

before_filter 'Thread.current[:host] = request.host_with_port'

def url_for(params)
   url_for_without_host(params.update(:host => Thread.current[:host]))
end
alias_method_chain :url_for, :host

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Accessing request.host in UserMailer

2009-01-09 Thread Mark Reginald James

Tom Hoen wrote:
> I am using subdomains in my application to control context. Therefor,
> during user registration, password reset, and other actions that require
> an email to be sent with a link to be clicked, I need to ensure that the
> subdomain appropriate for the user is used.
> 
> It seems I need to be able to access request.host from within an
> ActionMailer model. But I am not clear how to do this.

You'll have to supply the request object as a parameter to
mailer calls made in your controllers.

Here's an outline of one way to do this:

class MyMailer < ActionMailer::Base
   def initialize(method_name, request, *params)
 @host = request.host_with_port
 super(method_name, *params)
   end

   def url_for(params)
 url_for_without_host(params.update(:host => @host))
   end
   alias_method_chain :url_for, :host

   def doit(param1, param2)
 body :link => url_for(:controller => :user, :action => :rego)
   end
end


MyMailer.deliver_doit(request, param1, param2)


If you're not running multi-threaded you can instead
set the mailer default_url_options host in a before_filter,
allowing everything to work automatically.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: gem update problem

2009-01-09 Thread Mark Reginald James

Have you tried downloading the rubygems 1.3.1 source
from http://rubyforge.org/frs/?group_id=126 , unpacking,
then running "ruby setup.rb" as root in that directory?

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: What is wrong with validates_presence_of :my_association ?

2009-01-08 Thread Mark Reginald James

Ben Johnson wrote:
> Category.has_many :products
> 
> Let's say a product must belong to a category. I know it is recommended
> to do the following:
> 
> class Product
> validates_presence_of :category_id
> end
> 
> But, as we all know, this is a pain for new records, meaning what if the
> associated category is a new record?
> 
> I've been meaning to ask this, but what's wrong with doing the
> following:
> 
> class Product
> validates_presence_of :category
> end
> 
> I never understood why that was so wrong. So I wanted to ask and see if
> I am missing something. What do you think? Thanks for your help!

It isn't wrong, it's the right way to do it. You just have to
assign a category object when building a new Product.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: attachment_fu, deferred thumbnail li

2009-01-07 Thread Mark Reginald James

Elliott Blatt wrote:
> Hi,
> 
> I'm using attachment_fu with great success, but have run into a
> requirement that I'd like to solve elegantly --  but I've outstripped my
> ruby skills.
> I have a class that returns a hash of thumbnail definitions, like so:
> class Thumb
>def  self.get_thumbs(style)
>   return { :small => "100x50",  :large  => "200x100" }  if style
> == 'landscape'
>   return { :small => "50x100",  :large  => "100x200" }  if style
> == 'portrait'
>end
> end
> 
> My attachment model is defined like this:
> 
> class Photo < ActiveRecord::Base
>  has_attachment :content_type => :image,
>  :thumbnails => Thumb.get_thumbs(??)
>[...more stuff ...]
> end
> 
> My photos controller has the following line:
> 
>  @photo = Photo.new(params[:photo])
> 
> My question:
> 
> I'd like dynamically determine what value to set to Thumbnail.get_thumbs
> based on values set in the  Photo.new constructor.  Is this possible?
> I've fooled around with attr_accessors,  lambdas, etc, but, I can't seem
> to do what I want.   Is it possible to make such a run-time decision as
> I'm trying to make?
> 
> Which is to say, if params[:photo][:style] = 'portrait', i'd like to cut
> the thumbnails accordingly?
> 
> 
> Does anyone have an elegant solution for this?

A non-thread-safe solution may be:

after_save 'attachment_options[:thumbnails] = Thumb.get_thumbs(style)'
has_attachment :content_type => :image,
:thumbnails => Thumb.get_thumbs('landscape')

but a better solution may be to add PortraitPhoto and LandscapePhoto
sub-classes.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Empty collection returned for model with has_many relationship

2009-01-07 Thread Mark Reginald James

bigsley wrote:
> So is this a known bug? Is this fixed in 2.2.2? I'm currently running
> rails 2.1.1.

The issue is present in 2.2.2, and probably edge.
I'm looking into a fix, but as Fred says, there are
several subtleties. I suggest you watch this ticket:

http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1706

Assuming your console log was an attempt to investigate a problem
with some real app code, you should be able to work around the
problem by eliminating some unnecessary saves and some object
reuse. You can post your real code if you like.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Update_attributes on multiple models, revert back on error

2009-01-07 Thread Mark Reginald James

Terpinator wrote:

> I have one method, 'updateStuff', that will call 'update_attributes'
> on 3 different models in the DB.
> 
> How can I account for validation to revert back all updates if one of
> the models is not valid?

Note that you want all-or-nothing DB updates, but, for purposes
of form redisplay, always want both in-memory attribute updates
and validity-checking done on each. Here's how:

1. Update attributes on each without saving using the attributes= method.
2. Call valid? on each, storing each result in a local variable.
3. Proceed with transaction-wrapped saves if all three variables are true.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Empty collection returned for model with has_many relationship

2009-01-06 Thread Mark Reginald James

Frederick Cheung wrote:

>> Why does only the first object in the collection
>> have a different object_id, and why does this
>> difference go away if the empty a.steps is loaded
>> before b is added?  Is it a proxy thing?
>>
> Because .first will hit the database and load a fresh copy (if the
> collection is not already loaded). I waffled about this a bit a while
> ago: http://www.spacevatican.org/2008/11/15/first-foremost-and-0

Thanks Fred for pointing out the new behaviour of
"first" and "last".

However that's not the problem here. You get the same
behaviour if you use steps[0].

Looking at it again after some sleep, I see that the
different b object is actually loaded at the "a.steps << b"
line. That is, the object is added by updating the foreign key
in the database, loading the collection, then folding in any
new_records, which ends up giving you the different b from
the database.

The association_collection << method could easily be changed
to respect the identity of objects being added to a collection.
Should this be done to respect the POLS, violated for the OP?

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Empty collection returned for model with has_many relationship

2009-01-06 Thread Mark Reginald James

Frederick Cheung wrote:

> Even if it were setting it on the in memory object it wouldn't be in  
> memory object you think it is. Because a was reloaded, the objects in  
> a.steps are not the objects b & c (although they do correspond to the  
> same database rows).

The problem happens before a is reloaded.

I just did some experiments with puzzling results.
Would you be able to help explain them Fred?

Loading development environment (Rails 2.2.2)
 >> a = Exercise.find(1)
=> #
 >> b = Step.new
=> #
 >> b.save
=> true
 >> c = Step.new
=> #
 >> c.save
=> true
 >> a.steps << b
=> [#]
 >> a.steps << c
=> [#, #]
 >> b.object_id
=> 70254505869500
 >> a.steps.first.object_id
=> 70254505813160
 >> c.object_id
=> 70254505838840
 >> a.steps.last_object_id
=> 70254505838840

Why does only the first object in the collection
have a different object_id, and why does this
difference go away if the empty a.steps is loaded
before b is added?  Is it a proxy thing?

I added a few lines to the has_many code to set
the foreign keys on the in-memory records to nil.
But because of the above behaviour the fk doesn't change
on the local variable that held the first element added to
the collection, unless the collection had been pre-loaded.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Empty collection returned for model with has_many relationship

2009-01-06 Thread Mark Reginald James

Mark Reginald James wrote:

I take this back:

> It's only being manifest though because of the
> unnecessary saves you're doing, meaning that
> the problem has probably been rarely seen.

The problem has been exposed by the new ActiveRecord
change detection code.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Empty collection returned for model with has_many relationship

2009-01-06 Thread Mark Reginald James

bigsley wrote:
> I have two models:
> 
> Exercise, and Step. Exercise has many steps.
> 
> Basically, when I add a step to a new exercise's "steps" array, it
> saves, and everything works. If I then get the exercise from the model
> (via find), clear its array, then add the step again, I cannot save
> the step to the exercise. Here is a console dump which shows exactly
> what happens:
> 
> --
>>> a = Exercise.new
> => # created_at: nil,
>  updated_at: nil>
> 
>>> a.name = "The Name"
> => "The Name"
> 
>>> b = Step.new
> => # exercise_id: nil
> , created_at: nil, updated_at: nil, name: nil>
> 
>>> c = Step.new
> => # exercise_id: nil
> , created_at: nil, updated_at: nil, name: nil>
> 
>>> a.save
> => true
> 
>>> b.save
> => true
> 
>>> c.save
> => true
> 
>>> a.steps << b
> => [# exercise_id: 26,
>  created_at: "2009-01-06 02:26:36", updated_at: "2009-01-06 02:26:38",
> name: nil
>> ]
> 
>>> a.steps << c
> => [# exercise_id: 26,
>  created_at: "2009-01-06 02:26:36", updated_at: "2009-01-06 02:26:38",
> name: nil
>> , #> 26,
> created_at: "2009-01-06 02:26:36", updated_at: "2009-01-06 02:26:40",
> name: nil>
> ]
> 
>>> a.save
> => true
> 
>>> a = Exercise.find_by_name("The Name")
> => # created_at
> : "2009-01-06 02:26:35", updated_at: "2009-01-06 02:26:35">
> 
>>> a.steps
> => [# exercise_id: 26,
>  created_at: "2009-01-06 02:26:36", updated_at: "2009-01-06 02:26:38",
> name: nil
>> , #> 26,
> created_at: "2009-01-06 02:26:36", updated_at: "2009-01-06 02:26:40",
> name: nil>
> ]
> 
>>> a.steps.clear()
> => []
> 
>>> a.steps << b
> => [# exercise_id: 26,
>  created_at: "2009-01-06 02:26:36", updated_at: "2009-01-06 02:26:38",
> name: nil
>> ]
> 
>>> a.steps << c
> => [# exercise_id: 26,
>  created_at: "2009-01-06 02:26:36", updated_at: "2009-01-06 02:26:38",
> name: nil
>> , #> 26,
> created_at: "2009-01-06 02:26:36", updated_at: "2009-01-06 02:26:40",
> name: nil>
> ]
> 
>>> a.save
> => true
> 
>>> b.save
> => true
> 
>>> c.save
> => true
> 
>>> a = Exercise.find_by_name("The Name")
> 
> => # created_at
> : "2009-01-06 02:26:35", updated_at: "2009-01-06 02:26:35">
>>> a.steps
> => []
> 
> --
> 
> What is going on here? This is so anti-intuitive - how could a bug
> like this exist and not have been detected hundreds of times before?

Yes, I would regard this as a Rails bug.
The clear method is not setting the foreign key to
nil on the in-memory object.

It's only being manifest though because of the
unnecessary saves you're doing, meaning that
the problem has probably been rarely seen.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: backgroundrb stops suddenly

2009-01-02 Thread Mark Reginald James

Madhankumar Nagaraj wrote:
> hi,
> i have used backgroundrb to update the database with the files having
> some useful data, where i am getting files periodically uploaded.
> it works well, but suddenly it stops the worker and stopped the
> updation.
> when i checked the backgroundrb.log file
> it shows
> Mysql::Error: MySQL server has gone away: (Query used for updation)
> LIMIT 1 - (ActiveRecord::StatementInvalid)
> but, the query is working fine in mysql.
> 
> i have created one worker to do the updation with job_key.
> when i checked the job_key value for the particular worker is shows nil.
> since the worker was stopped.
> 
> what would be the problem, how can i make the updation works
> continuously.

Your MySQL session is timing-out after the default 8 hours.

Either increase the timeout by adding the following
line to the [mysqld] section of the /etc/my.cnf file:

   set-variable = wait_timeout=<#seconds>

or you can instead insert a call to

   ActiveRecord::Base.verify_active_connections!

ahead of any database queries.

The verify_active_connections! method is called by
Rails before every request, but your backgroundrb
process will not be getting this constant hammering
to keep its database connection alive.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Why are JS requests using layouts?

2009-01-02 Thread Mark Reginald James

Ben Johnson wrote:
> If I specify a layout in a controller:
> 
> class MyController < ApplicationController
> layout "some_layout"
> end
> 
> Any JS request will use the some_layout. I thought certain requests
> types were exempt from layouts? When I remove the layout call everything
> works perfect, the problem is that it uses the application layout.
> 
> Any ideas why this is or how I can fix this?

Is this with a recent version of Rails?
At one time you had to do

layout proc {|controller| 'some_layout' unless controller.request.xhr?}

But if you're doing proper RJS renders, I don't think it's now necessary.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: link_to_remote can't replace specified element?

2009-01-02 Thread Mark Reginald James

JHuizingh wrote:
> I'm having an issue with the design of my views that I can't seem to
> wrap my head around.  I have a page where I am showing a collection of
> objects in a list.  The way that I designed it, it looked something
> like this:
> 
> main view:
> 
> here is the list
> 
> <%= render(:partial => "items/item", :collection => @items %>
> 
> 
> 
> collection item's view:
> 
>  
>  ...  INFO ABOUT item
> 
> 
> 
> This works great, creating a  that is full of 's.  I hit a
> snag when I start trying to update individual  items with ajax
> calls.  The :update option for  link_to_remote() and related functions
> doesn't give me an option to replace the dom element that it refers to
> as far as I can tell.  It lets me specify a dom id that I can change
> the innerHTML of, insert the returned html with in it at the beginning
> or the end, before or after the element, but it doesn't seem to allow
> me to REPLACE the element.
> 
> So a call like this:
> 
> remote_function(:url => credential_search_instance_path
> (credential_search_instance),
>  :method => :put,
>  :update => dom_id(item)
>)
> 
> Will replace the html inside of the  element, so it would update
> the page to look like this:
> 
>  
>  
>  ...  INFO ABOUT item
> 
> 
> 
> 
> I ended up getting around the issue by checking if it is an xhr
> request in my item view like this;
> <% if ! request.xhr? then %>
>  
> <% end %>
>  ...  INFO ABOUT item
> <% if ! request.xhr? then %>
> 
> <% end %>
> 
> That solution works, but it seems very inelegant and adds a lot of
> noise to the view.  Is it really true that I can't replace the dom
> element?  Am I missing something?  Is there a better way to design my

The best solution would be to move the li tags to a layout:

render :partial => "items/item", :collection => @items, :layout => 'li'

Where _li.html.erb contains

<%= yield %>

However this is broken in 2.2.2 (it instead renders a collection
of the collection). I've created a ticket:
http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1688

So until this works, the other options are:

1. Replace the collection with a loop, either using the
layout option, or putting the li tags in the main view,

2. Create a second level of partial, or

3. Remove the :update option and use outer-HTML replace RJS calls.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Finding Child Records Efficiently

2008-12-30 Thread Mark Reginald James

Mark Reginald James wrote:

> os = sanitize("/#{old_name}/")
> ns = sanitize("/#{new_name}/")

Whoops, you'd need to add the prefix /path/to on these
to ensure you only replace the name in the correct context.

> Person.update_all "key = replace(key, #{os}, #{ns})",
>   ['key like ?', "/path/to/#{old_name}/%"]

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: when to set a class attribute variable during boot

2008-12-30 Thread Mark Reginald James

Daniel Choi wrote:
> I have an ActiveRecord model with a cattr_accessor. The class
> attribute is set up like this:
> 
> class MyModel < ActiveRecord::Base
>   cattr_accessor :my_attribute
> end
> 
> Because I need to give #my_attribute environment-specific values, I
> try to set this attribute in environments/development.rb like so:
> 
> MyModel.my_attribute = 3
> 
> But this leads to odd and erratic behavior when #my_attribute is
> called from a controller. Sometimes MyModel.my_attribute returns 3,
> but sometimes it returns nil.

In development mode, models get reloaded on every request,
so your variable won't persist. You'll have to set it in
in a persistent class or module, in the global context,
or in the database or session.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Finding Child Records Efficiently

2008-12-30 Thread Mark Reginald James

DAZ wrote:

> I guess that keeping the ancestors as a string in the database makes
> more sense than I first thought. It is interesting that Robert refers
> to this as 'caching' the family-tree, which obviously it is, but I've
> not really thought of caching information like this (ie database level
> relationships) before. Consequently, as Mark says it needs to be
> updated whenever a name changes, which shouldn't be too difficult to
> do (cycle through all the descendants and update that row?).

Yes, you could use a recursive (tree) or batch (nested-set) traverse.
Or could actually do it in one statement, like

os = sanitize("/#{old_name}/")
ns = sanitize("/#{new_name}/")
Person.update_all "key = replace(key, #{os}, #{ns})",
  ['key like ?', "/path/to/#{old_name}/%"]


> This has the added advantage of acting as a sort of permalink row as
> well. Thinking about pretty urls, it wouldn't be too hard to use this
> string to generate:
> 
> http://appname/people/abe/homer/bart
> 
> This would be the url that took you directly to Bart's url. Could I
> use the '/' character as a separator at the DB level?

Yes, good idea.

> Mark - I'm not sure what you mean by this:
>> you should put the name match into the
>> SQL conditions rather than in a Ruby loop.
> 
> I guess that you mean to use SQL rather than looping through ruby
> arrays, but I'm afraid my SQL is very poor - I usually only use the
> Rails helpers to do basic stuff.  I'm struggling to follow your
> example, but I guess that it corresponds to what Philip was alluding
> to by saying to construct a joins string?

person.children.find(:conditions => ['name = ?', name])


-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: index => nil params jumbled *sometimes*

2008-12-30 Thread Mark Reginald James

Matt Darby wrote:
>> Those first three fields are not enclosed in a cell.
>> That may cause IE to send the parameters in the wrong order.
>>
>> Please log and post the raw_post string.
> 
> Ahh, IE, my old friend. Perhaps this is why I wasn't able to recreate
> it.

Well, I can't blame IE for tripping up on invalid HTML.

> Here is the raw POST:
> 
> Processing TimeCardsController#day (for 192.168.1.192 at 2008-12-18
> 09:14:13) [POST]
> Parameters: {"date"=>"2008-12-17", "time_cards"=>[{"job_id"=>"3107",
> "time_card_entry_type"=>"Field", "time_card_date"=>"2008-12-17",

What you really need to look at is not the params hash,
but the string posted by the browser. See this by adding
"logger.info request.raw_post" to your controller action.

If the parameters are indeed posted out of order, then the
problem was with the missing td tags. However a correct
order would instead suggest a bug in Rails' parameter processing.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: changing row color on checkbox click

2008-12-29 Thread Mark Reginald James

Scott Kulik wrote:
> say for example i had something simple like this in a view:
> 
> <%= check_box_tag(:task) %>
> 
> how would i add onclick to change the row color on a check and when the
> check is removed?
> 
> maybe something like this?
> 
> page[task].addClassName('complete')
> 
> i'm not that familiar with javascript so i'm not sure how i would finish
> this.  thanks!

<%= check_box_tag :task, 1, false,
:onchange => "$(parentNode.parentNode).toggleClassName('complete')" %>

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Finding Child Records Efficiently

2008-12-29 Thread Mark Reginald James

DAZ wrote:
> On Dec 28, 8:07 pm, Mark Reginald James  wrote:
> 
>> One alternative is to make the ancestor array a string key
>> to each record ("abe|homer|bart"), allowing instant retrieval.
> 
> This seems like a relatively good idea, could have a string-key called
> family_tree or something and just do find_by_family_tree("abe|homer|
> bart")
> This doesn't quite feel right - it seems like the only info you should
> need to keep is a person's parent (from which you can then find their
> parent and so forth). It might also lead to some very long strings
> eventually!

It does require maintenance: whenever a name changes you have to
update the string in all children and all ancestors.

>> Another would be to build the sql iteratively:
>>
> 
> This is what I'm doing at the moment - I'm using the "betternestedset"
> plugin, so have access to a "children" method that returns an arrary
> of children. Will this have all been pre-fetched efficiently? And if
> so, is the iterative code the way to do it?
> 
>tree = ["abe","homer","bart"]
> person = Person.find_by_name(tree[0])
>   if tree.size > 1
> 1.upto(tree.size - 1) do |i|
>   person = person.children.find { |child| child.name == tree
> [i] }
> end
>   end
> @person = person

First-generation children are efficiently retrieved in both
better_nested_set and acts_as_tree via the parent key.
better_nested_set also allows you to efficiently retrieve all
generations of children using the all_children method.

So your current method will be making one DB call per
generation. And you should put the name match into the
SQL conditions rather than in a Ruby loop.

But have a look at the code in my last post. It can find
the correct Bart using only one DB call by matching
up the unique ancestors chain.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: index => nil params jumbled *sometimes*

2008-12-29 Thread Mark Reginald James

Matt Darby wrote:

> 
>   
>type="hidden" value="121" />
>   
> 
>   

Those first three fields are not enclosed in a cell.
That may cause IE to send the parameters in the wrong order.

Please log and post the raw_post string.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Best way to automatically format model entries

2008-12-28 Thread Mark Reginald James

Richard Schneeman wrote:
> Using rails 2.1.0 I have a really nasty bit of code in my controller in
> my create action that i would like to move to my model:
> 
> params[:phrase]["word"] = params[:phrase]["word"].strip.squeeze(" ")
> if params[:phrase]["word"] != nil
> 
> params[:phrase]["second_word"] =
> params[:phrase]["second_word"].strip.squeeze(" ")  if
> params[:phrase]["second_word"] != nil
> 
> params[:phrase]["third_word"] =
> params[:phrase]["third_word"].strip.squeeze(" ")   if
> params[:phrase]["third_word"] != nil
> 
> @phrase = Phrase.new(params[:phrase])
> 
> 
> What would be the best way to do this, I was thinking of putting it
> inside of a :before_save function. But i've never written one before and
> didn't know how to get the parameters from my recently saved phrase( how
> do I tell it to use the phrase i just created? @phrase.word ? ). What
> would you recommend??

Put something like this in a file in the lib directory:

class ActiveRecord::Base
   def self.blacken_attrs(*attrs)
 before_validation <<-END
   #{attrs.inspect}.each do |attr|
 if v = send(attr)
   v.strip!
   v.squeeze!(' ')
 end
   end
 END
   end
end

Then in the Phrase model write

blacken_attrs :word, :second_word, :third_word

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Finding Child Records Efficiently

2008-12-28 Thread Mark Reginald James

DAZ wrote:
> I have a family-tree structure to a person model.
> 
> I want to be able to find people by specifying an array that
> corresponds to the family tree. So if Abe is grandpa, homer is dad and
> bart is the son, bart's array would be 
> 
> To find bart, I can't just use find_by_name("bart") as there might be
> another person called bart (with differnt parents and therefore a
> different array). So, to find this particular bart, I want to find abe
> first, then find homer, then bart.
> 
> How can I do an ititial database query of find_by_name("abe") that
> will find the "abe" record as well as all his descendant records, so
> that any subsequent filtering is efficient and doesn't hit the
> database?

Efficient retrieval of all descendants or all ancestors
of a record requires the addition of nested-set capability.

One alternative is to make the ancestor array a string key
to each record ("abe|homer|bart"), allowing instant retrieval.

Another would be to build the sql iteratively:

ancestors = %w(abe homer bart).reverse
conds = {"people.name" => ancestors.shift}
joins = nil
ancestors.each_with_index do |name, i|
   joins = joins.nil? ? :parent : {:parent => joins}
   conds.update("#parents_people#{'_' + (i+1).to_s if i!=0}.name" => name)
end

bart = Person.find(:first, :conditions => conds, :joins => joins)

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Help with nested models and fields_for

2008-12-23 Thread Mark Reginald James

Mark Reginald James wrote:

> Below is a patch that seems to fix the problem.  I haven't
> yet run the rails test suite on it, nor added a test that would
> have broken the original.

I've confirmed that this patch fixes the problem without regression.

http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1622-incorrect-parsing-of-query-parameters-with-mixed-depth-nesting-inside-an-array

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: ror plugins - in_place_editing and acts_as-textiled - install error

2008-12-23 Thread Mark Reginald James

Frederick Cheung wrote:

> You can download tarballs/zip files off github.

Ah, thanks Fred. I've added ZIP download links to
those plugin listings.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Help with nested models and fields_for

2008-12-22 Thread Mark Reginald James

Marnen Laibow-Koser wrote:

> Thanks for the suggestion.  I tried the same test, and got interesting
> if disheartening results.  When I had recipe[ingredient_lines][]
> [ingredient][name] as the only form field, I got the same results as
> you.  But the moment I added recipe[ingredient_lines][][unit],
> [ingredient][name] stopped working.  This was true both with Rails
> 2.1.2 and 2.2.2.  I think I may be running into a bug in Rails.

Thanks for running that test.

Below is a patch that seems to fix the problem.  I haven't
yet run the rails test suite on it, nor added a test that would
have broken the original.

Just run the patch command in actionpack-2.2.2/lib/action_controller

--- request.rb  2008-12-23 13:07:00.0 +1100
+++ request.rb.new  2008-12-23 14:37:30.0 +1100
@@ -811,9 +811,9 @@
top[-1][key] = value
  else
top << {key => value}.with_indifferent_access
-  push top.last
-  value = top[key]
  end
+push top.last
+value = top[key]
else
  top << value
end

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Callback on update but not on destroy

2008-12-22 Thread Mark Reginald James

jeroen wrote:

> Does anyone know if there's a callback that gets called when a
> existing record gets updated but not when a record gets destroyed?
> After_update and before_update seem to also get called on destroy. I
> there is no such a callback can anybody suggest an alternative way of
> triggering a method when a record gets updated unless the record gets
> destroyed?

The update callbacks aren't called on destroy.
Are you sure you're not doing an update you're not aware of.
Can you see such an update in your SQL logs?

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: ror plugins - in_place_editing and acts_as-textiled - install error

2008-12-22 Thread Mark Reginald James

suman gurung wrote:

> I was trying to install these in_place_editing and acts_as_textiled
> plugins in my rails application version 2.2.2, running on windows XP
> but i came across some problems when doing so.
> ...
> Has the plugins been moved to a different directory?? i believe these
> are fairly popular plugins, so i hope i can get some help here.

Both have now been moved to git.

Here are installation instructions:
   http://railswheels.com/acts-as-textiled
   http://railswheels.com/in-place-editing

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Help with nested models and fields_for

2008-12-21 Thread Mark Reginald James

Marnen Laibow-Koser wrote:
> 
> On Dec 21, 11:42 am, Mark Reginald James  wrote:
>> Marnen Laibow-Koser wrote:
>>>> It can't handle more that one level below an array parameter.
>>> Are you sure?  I've seen a few examples on the Web which imply that
>>> this should work.
>> I can confirm that it's broken in Rails 2.0.2, and fixed
>> in Rails >= 2.1.0.
> 
> Then something else is going on.  I'm using Rails 2.2.2 and having
> these problems.
> 
> [...]
>> objects[][foo] works, but objects[][foo][bar] did not.
> 
> And still apparently does not.  Any other thoughts?

The test I did was to post from the following view.





On Rails >= 2.1 I got the correct
"recipe"=>{"ingredient_lines"=>[{"ingredient"=>{"name"=>"hhh"}}]}

on 2.0.2 I got what you're seeing:
"recipe"=>{"ingredient_lines"=>[{"ingredient"=>{}}]}

You may like to run the same test.

If you see the same, perhaps the problem manifests when extra
parameters are added. You can try adding fields until you
are posting your complete form.  Please post what you find out.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: how to reference lookup data in DB table - by ID? (or should the ID not be relied upon)

2008-12-21 Thread Mark Reginald James

Greg Hauptmann wrote:
> Hi,
> 
> What's a good approach for referencing lookup/reference data (static) 
> that one has loaded in a reference table.  Say for example "tax codes", 
> which pretty much have just the database ID and then a description 
> field.  Some options that come to mind:
> 
> 1 - By ID - however this makes an assumption that the data always loads 
> into a database and gets the same IDs (i.e. when you're loading your 
> static reference data)?
> 
> 2 - Search by description to identify the record - but obviously if one 
> changes the description this isn't very good.
> 
> 3 - Perhaps create a specific identify/key as a new column that you use, 
> but is not the database Rails ID?  How's this sound?

I'd recommend the enumerations_mixin plugin that both caches the
table and allows lookup and by id and (unique) name. e.g.

self.tax_code = :exempt
if tax_code === :exempt
   ...

http://svn.protocool.com/public/plugins/enumerations_mixin/

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Help with nested models and fields_for

2008-12-21 Thread Mark Reginald James

Marnen Laibow-Koser wrote:

>> It can't handle more that one level below an array parameter.
> 
> Are you sure?  I've seen a few examples on the Web which imply that
> this should work.

I can confirm that it's broken in Rails 2.0.2, and fixed
in Rails >= 2.1.0.

> By "array parameter", do you specifically mean one with a numeric
> subscript, so that object[foo][bar][baz] would work as expected,
> although objects[][foo][bar][baz] would not?  If so, that would
> explain some -- not all -- of the examples I've seen.

objects[][foo] works, but objects[][foo][bar] did not.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Help with nested models and fields_for

2008-12-20 Thread Mark Reginald James

Marnen Laibow-Koser wrote:

>   
> 
> So far so good.  But here's the problem: *submissions don't work quite
> properly*.  When I fill out the form and submit it, I get params
> entries like
> "recipe"=>{"name"=>"Recipe", "ingredient_lines"=>[{"quantity"=>"1",
> "unit"=>"cup", "ingredient"=>{}}], "instructions"=>"Preheat oven to
> 350°..."}. Note that ingredient_lines[0].ingredient.name is missing,
> even if I enter text in the corresponding form field -- in other
> words, what I should be seeing is something like "recipe"=>
> {"name"=>"Recipe", "ingredient_lines"=>[{"quantity"=>"1",
> "unit"=>"cup", "ingredient"=>{"name" => "flour"}}],
> "instructions"=>"Preheat oven to 350°..."}.

I believe this is a limitation of Rails' parameter parsing.
It can't handle more that one level below an array parameter.

A solution is to create an ingredient_name accessor in
IngredientLine, and arrange to have this rendered and posted.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: uninitialized constant BooksController::Books

2008-12-19 Thread Mark Reginald James

Newone One wrote:
> hi new to rails ...
> wat does it means...
> uninitialized constant BooksController::Books
> 
> /usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:102:in
> `const_missing'
> app/controllers/books_controller.rb:5:in `index'

Most likely you have written Books.find instead of Book.find.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Setting a CSS class on select helper

2008-12-19 Thread Mark Reginald James

Dan Weaver wrote:
> Hi,
> 
> I'm trying to put a css class on a dropdown box built using a select
> helper (to make it wider) but I can't get it to take.  I thought for
> sure, and from what I've seen elsewhere, this code should work:
> 
> <%= f.select :month, 1..12, :selected => Time.now.month.to_i, :class =>
> 'monthSelect' %>

You have to make sure Ruby sees two separate hash arguments:

<%= f.select :month, 1..12, {:selected => Time.now.month.to_i},
 :class => 'monthSelect' %>

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: how to do "special queries" ala The Rails Way on "associated" conditions?

2008-12-17 Thread Mark Reginald James

lunaclaire wrote:
> Thanks for the reply, Mark.
> 
> One of the assertions in that RailsWay posting was that by using the
> has_many approach, I'd gain efficiency because of caching. I don't
> understand enough about caching yet to see why, but let me ask this
> about using your approach... would the code you show with the :joins
> clause be more or less efficient than the code I have above in my
> current home_stats method?

Your original code becomes more efficient if:
- Both visitor and home stats are often used together in the same request
- The collections are small

But because you were not pre-loading the the player and team
associations using :include, what you had is very inefficient.
Each time round each loop a DB fetch is being made for the
player of a stat record and the team of a player record.

You could add caching to the method I posted:

def home_stats(reload = false)
   @home_stats = nil if reload
   @home_stats ||= player_stats.find ...
end


-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Callback when objects connect as a habtm relationship

2008-12-17 Thread Mark Reginald James

Kazim Zaidi wrote:
> If Product and Category models are in a habtm relationship, i.e.
> 
> class Product < ActiveRecord::Base
>   has_and_belongs_to_many :categories
> end
> 
> class Category < ActiveRecord::Base
>   has_and_belongs_to_many :products
> end
> 
> I want a piece of code to be executed every time a product is connected 
> to a category.
> Where do I put this code? Which callback (and on which model) will be 
> triggered?

Look into adding :after_add callbacks to the associations:

http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M001602


-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: conditional delete of a session

2008-12-17 Thread Mark Reginald James

James Byrne wrote:
> Rails 2.2.2
> Ruby 1.8.6
> 
> ActiveRecord Session Store enabled
> 
> For testing purposes I wish to ensure that no session exists.  To
> accomplish this I wrote:
> 
>   session.delete if session
> 
> But this fails if session == nil and I suspect that it does not work as
> I anticipate if session exists.  In the console I see this behaviour:
> 
> $ script/console
> Loading development environment (Rails 2.2.2)
>>> app.session
> NoMethodError: You have a nil object when you didn't expect it!
> The error occurred while evaluating nil.session
> from
> /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/test_process.rb:429:in
> `session'
> from (irb):1
> 
>>> app.session.delete if app.session
> NoMethodError: You have a nil object when you didn't expect it!
> The error occurred while evaluating nil.session
> from
> /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/test_process.rb:429:in
> `session'
> from (irb):2
> 
>>> app.get "/"
> => 200
> 
>>> app.session
> => # @dbman=# 
>>> app.session.delete if app.session
> => []
> 
>>> app.session
> => # @dbman=# 
> Now, I believed that nil == false and that the construct "object.method
> if object" should work.  Evidently I am misinformed or there is
> something exceptional about session that I do not know.
> 
> How is what I desire, to remove the session if one already exists,
> accomplished?

Perhaps

session.delete if defined?(session) && session

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: require file in rails

2008-12-17 Thread Mark Reginald James

Kaushik katari wrote:
> Hi,
> I have a file called dbcon.rb that I put in the config directory.
> 
> I want to call it from rake, and I was using
> require 'config/dbcon.rb'
> 
> This was allowing me to access the classes in this file.
> 
> Somehow, the require command cannot find this file anymore. Is there a 
> path convention in rails? How do I use require from different parts of 
> my application?

You could move it to the lib directory, or write
   require "#{RAILS_ROOT}/config/dbcon.rb"

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: session In the model

2008-12-17 Thread Mark Reginald James

Sijo Kg wrote:
> Hi
>i have the model SDTicket which has a field modified_by_id and it has
> to take data from session[:id]  Now this SDTicket model has many
> associated models like Activity one of them So when ever an activity
> happens then also the value in session[:id] is to be go to
> modified_by_id field
> 
> SDTicket has_many activities
> Activity belongs to SDTicket
> 
>  So for that what I am trying is in Activity model wrote in
> after_save like below
> after_save :update_sd_ticket
> def update_sd_ticket
> #Here I dont know how to get that session[:id] and fill that to
> modified_by_id
>  self.service_desk_ticket.save
> end
> 
>I also tried like to get session from a module by including it in
> activity model But that too not working.Could you please help to solve
> this

The session hash is only available in the controller and view.

You'll have to add a line to your controller method before your
call to save

   activity.modified_by_id = session[:id]

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: how to do "special queries" ala The Rails Way on "associated" conditions?

2008-12-17 Thread Mark Reginald James

lunaclaire wrote:
> I have the following in a model class:
> 
> class Game < ActiveRecord::Base
>   has_many :player_stats
>   has_many :players, :through => :player_stats
> 
>   def visitor_stats
>   stats = []
>   player_stats.each do |stat|
>   stats << stat   if stat.player.team.team_code ==
> self.visiting_team_code
>   end
>   stats
>   end
> 
>   def home_stats
>   stats = []
>   player_stats.each do |stat|
>   stats << stat   if stat.player.team.team_code == 
> self.home_team_code
>   end
>   stats
>   end
> end
> 
> I understand that this isn't the most efficient or elegant way to do
> things. I gleaned this  most recently from the posting at (see his
> Case 3):
> 
> http://www.therailsway.com/tags/has_many
> 
> So, I would like to do something like the following, but I'm having a
> problem with the conditions:
> 
>   has_many :visitor_stats, :class_name=>"PlayerStat", :conditions=>
> 
> 
> the condition here should be something matching "player.team.team_code
> == self.visiting_team_code" in the code I have now.
> 
>   has_many :home_stats, :class_name=>"PlayerStat", :conditions=>
> 
> 
> and here it should be something matching "player.team.team_code ==
> self.home_team_code" in the existing code.
> 
> The conditions specified need to be something that could go in a SQL
> statement, right? How would I make the conditions work thru the
> associations of a PlayerStat belonging to a Player belonging to a Team
> which has a team_code. And matching it to this Game's
> visiting_team_code or home_team_code

I don't think what you need can be formulated into normal has_manys.
You'll either have to use the has_many :finder_sql option that
includes joins to the players and teams tables, allowing you to
add the appropriate SQL conditions, or you can keep visitor_stats
and home_stats as methods, but have them call find:

def home_stats
   player_stats.find :all, :select => 'player_stats.*',
 :joins => {:player => :team},
 :conditions => "teams.team_code = #{home_team_code}"
end

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: one association from two

2008-12-17 Thread Mark Reginald James

dschruth wrote:
> I'm working on a two-to-many relationship that works fine from the
> child object's perspective but not from the parent's.The
> imperfect, yet functional code looks like this:
> ---
> class PrimaryTable < ActiveRecord::Base
>   has_many :secondary_tables_1 ,
> :foreign_key => :A_primary_table_id,
>:class_name => "SecondaryTable"
>  has_many :secondary_tables_2 ,
>:foreign_key => :B_primary_table_id,
>:class_name => "SecondaryTable"
> end
> 
> class SecondaryTable < ActiveRecord::Base
>   belongs_to :A_primary_table,
>:foreign_key => :A_primary_table_id,
>:class_name => "PrimaryTable"
>   belongs_to :B_primary_table,
>:foreign_key=> :B_primary_table_id,
> :class_name => "PrimaryTable"
> end
> --
> 
> But, I don't want to have two linking fields up *and* two linking
> fields down.  I only want one linking field down.  I would like to
> lump these parent to child relationships into one associated link...
> doing something like this:
> 
> --
> 
> class PrimaryTable < ActiveRecord::Base
>   has_many :secondary_tables ,
>:foreign_key => :A_primary_table_id,
>:foreign_key => :B_primary_table_id,
>:class_name => "SecondaryTable"
> end
> 
> -
> 
> ...but only one of the two links (the ones to "B_primary_table") shows
> up this way.

Using the :finder_sql option to has_many, you'll have to write
custom SQL that does an "or" on the two foreign keys.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Active Record - find - select option

2008-12-11 Thread Mark Reginald James

Bharat Ruparel wrote:

> Coach.find :all, :select => 'c.id, a.id, a.name',
>:joins => 'as c inner join accounts as a on a.id =
> c.account_id',
>:order => 'a.name'
> 
> It is only returning the following in irb:
> 
>  [#]
> 
> That is, it fails to return the account.id (or a.id) and account.name
> (or a.name)

Given that you're not using :include => :account for efficiency reasons,
you have to ensure that the account id does not override the coach id:

Coach.find :all, :joins => :account, :order => 'accounts.name',
   :select => 'coaches.id, accounts.id as account_id, accounts.name'

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Get an attrifutes from joins in a find method

2008-12-11 Thread Mark Reginald James

Luca Roma wrote:
> I have wrote an example:
> 
> example=Example.find(:all,:joins=>"(select id,"hello" as hello_name from
> examples2) as examples3 where examples3.id=examples.id")
> 
> I want get the attribute hello_name that not exits in db.
> example[0].hello_word not works
> example[0].example3.hello_name not works

Try example[0][:hello_word]

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: how do I get the host or Ip address where my rails app is running

2008-12-11 Thread Mark Reginald James

Erwin wrote:

> I would like to get in a variable the ip address of the host where my
> app is running, to change some parameters if it's running locally or
> remotely .. is it possible ?

You can try

require 'socket'
REAL_PRODUCTION = Socket.gethostname == 'mysite.com'

or

hostinfo = Socket.gethostbyname(Socket.gethostname)
hostnames = hostinfo[1] << hostinfo[0]
REAL_PRODUCTION = hostnames.include?('mysite.com')

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: find with add of an atttributes

2008-12-09 Thread Mark Reginald James

Luca Roma wrote:
> I have wrote this:
> EventDate.find(:all,
> :include => [:container,{:event =>
> [:photo,:event_type_names]},{:place=>:location}],
> :joins =>" RIGHT JOIN (SELECT
> event_dates.id,date_add(event_dates.date, INTERVAL i DAY ) AS date
>   FROM (select 0 as i  union all select 0 union all select 1
> union all select 2) as integers CROSS JOIN event_dates
> WHERE i<= number_days AND (event_dates.days_of_week='8'
> OR concat('%',event_dates.days_of_week,'%') like
> concat('%',DATE_FORMAT(date_add( event_dates.date, INTERVAL i DAY
> ),'%w'),'%'))
>  ORDER BY date LIMIT 0,40) as date_days ON date_days.id=event_dates.id
> ",
> :order=>"date_days.date"
> 
> I want select also the date_days.date attribute.
> Is there any mode to do it?

All the date_days fields will be selected by default,
the "date" field being available in each result as
event_date[:date].

You're in trouble if this conflicts with an EventDate
attribute, because Active Records has ignored the
find :select option when eager loading is used (though
I'm not sure whether this is still the case for Rails 2.2).
A patch is available for Rails 2.0.2 that allows :select
to be used in these circumstances.

Are you sure you want to use a right join? This could
give Active Record trouble.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Get a collection using an array of ids, keeping the order

2008-12-04 Thread Mark Reginald James

If you use MySQL, the "field" function allows you
to retrieve records in a specific id order:

http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/158dc2d879b2fb1/af78ce75ddfa1ed1

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Need Help: Deployment Issues with Voting App?

2008-12-03 Thread Mark Reginald James

Leximo wrote:

> Taking a look at this, I can see that the votes not being created on
> the production server. Maybe something is wrong with MySQL??

It's normal not to see database activity in production log.

Perhaps you should use create! rather than create to ensure
that there are no problems inserting the vote.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: ferret question - need help with syntax for exact matching

2008-11-29 Thread Mark Reginald James

jamie wrote:

> I have a need for a layer of access control when performing a query on
> the ferret index.  The solution is to add an dditional clause (e.g. -
> access_teams:12 OR 13 OR 14 OR 22 OR 1).  In order for this to work, I
> need exact matching. This query does not perform an exact match. It
> appears if the index record has access_teams=2  the query will return
> this record.
> 
> Can someone help me with the syntax for exact matching?

Perhaps

+access_teams:12|13|14|22|1


-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: local variables with hashes

2008-11-29 Thread Mark Reginald James

Pentti Laitinen wrote:
> I have a question about using local variables as values for ruby hashes.
> I have to do a script that will gather meta data of video files in a
> fixed directory and store them in an array of hashes. The problem is
> that when I have collected the meta data to a hash called video and
> append this to the end of videos array all the hash entries are the same
> in the array. I assign the values to the hash and append to the videos
> array with this script block:
> 
> video["res_w"] = res_w
> video["res_h"] = res_h
> video["dur_h"] = dur_h
> video["dur_m"] = dur_m
> video["dur_s"] = dur_s
> video["dur_ms"] = dur_ms
> videos << video
> 
> All the local variables have been initialized as zeroes and when my loop
> retrieves another file for processing it reuses the local variables. The
> actual question is does the hash store a pointer to the local variable
> as its value? If it does is there a way to overwrite this behaviour or
> some way around it?

Integer hash values aren't stored as object references.

Perhaps your problem is that you're not re-initializing
video to {} each time around the loop, so you're always
updating a common video object.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: eager loading optimization

2008-11-15 Thread Mark Reginald James

Richard Schneeman wrote:
> I have a phrase model, a definition model, and a children model
> a phrase has many definitions, a definition has many children
> 
> currently when i am doing a find i have an include that looks like
> 
> Phrase.find(:all,:include => [{:definitions => :children}] , :conditions
> => 'foo')
> 
> but for the most part I only want the first definition, and its
> associated children. The loading of all of the additional definitions
> and their associated children is killing my site load time (but not more
> than eliminating the :include all together).
> 
> Is there a way to get only the first definition under an associated
> phrase ( the definitions are ordered by rank) without having to write my
> own SQL ??
> 
> Rails 2.1.0 and Ruby 1.8.6

Tricky, but investigate something like:

class Phrase < ActiveRecord::Base
   has_one :top_ranked_definition, :class_name => 'Definition',
   :conditions => <<-END
 definitions.rank =
 (select max(rank) from definitions where phrase_id = phrases.id)
   END

Phrase.find :all, :include => {:top_ranked_definition => :children},
   :conditions => 'foo'

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: How to proxy streaming video

2008-11-14 Thread Mark Reginald James

Void wrote:
> We have a secure application on the Internet that you have to log into
> to use.  We want to be able to display a video from a streaming media
> server via this application.  The streaming media server is behind a
> firewall so I want my Rails app to proxy the stream to the user so
> that the stream is not available unless you log in.
> 
> I know I can use send_file to just show the video file, but I prefer
> to be able to use the streaming server so that the user does not have
> to wait for the file to download before it starts to play.

You could do this by having the action read from a streaming
Net::HTTP get, while writing the fragments to Rails' output
buffer.

Have you also looked into adding a one-time authentication
key to URLs passed to the streaming server?

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: dynamic condition for has_one and eager loading issue

2008-11-14 Thread Mark Reginald James

[EMAIL PROTECTED] wrote:
> What might happen in multithreaded mode??

Any dynamic change to the conditions of an association
would be common to all Rails handlers associated with that
process. Prior to Rails 2.2, this is was always one, so
there was no problem. But if you use Rails in multi-threaded
mode, one thread could change the conditions used by another
thread before those conditions are built into a DB query.

So you'd either have to put an exclusive lock around
that query (which may have a significant impact on the
database parallelism), or rewrite the AR code to fork
a thread-private condition whenever one is dynamically
set.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: caching for rjs?

2008-11-14 Thread Mark Reginald James

David wrote:
> I am using rjs to perform ajax deletes on objects within an array
> @instructions.  The objects within the array @instructions vary
> depending on some other logic in my controller and is not dependent on
> direct correlations within the database.  In other words, when I
> update the partial with:
> 
> page.replace_html :instructions, :partial =>
> 'instructions', :collection => @instructions
> 
> The @instructions cannot just be populated by active record
> correlations such as:
> @day.instructions.
> 
> The logic used in the controller to populate the @instructions
> variable is expensive and I would prefer not to go through the logic
> again if possible.  What is the best option for this situation, would
> using some caching method be best, or is there a way to store the
> @instructions variable in a flash or params or something?

Probably the best way would be to cache the whole HTML
partial using Rails' partial-cache capability. You would
use a custom cache key that's built on the set of controller
parameters that influence the contents of the @instructions
collection.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: dynamic condition for has_one and eager loading issue

2008-11-12 Thread Mark Reginald James

[EMAIL PROTECTED] wrote:
> Hi,
> 
> I ve defined the following relation in one of my models with a dynamic
> where condition:
> has_one :selection,
>   :foreign_key => 'object_id',
>   :conditions => 'selection_type = 1 and account_id = #
> {self.send(:account_id)}'
> 
> That works perfect, however when I try to eager load that relation I
> am getting the following error when doing a count. Seems that self is
> not really my model class anymore Does anybody know how I can get
> that up and running?

http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/7f7f715d917b9faa/426c768dd282929f

But be careful if and when you use Rails in multi-threaded mode.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: AcriveRecord (2.2.0) after_update callback and database transaction

2008-11-10 Thread Mark Reginald James

Hubert £êpicki wrote:

> Does anyone know how can I execute some bit of code after transaction
> wrapping up activerecord update/save methods completed?
> 
> after_update and after_save filters are both wrapped into the same
> transaction, and I need to make external service (druby) aware of
> changes immediately after changes were made. Now - I use separate
> thread and sleep(1) in druby service to make it see changes, but it's
> not good enough solution. Any ideas how can do it?

Like you, I've used the sleep hack, because ActiveRecord
doesn't currently have after_commit callbacks.

The only other solution would be to move the remote
call to your controller, after the transaction block
or save call.  You can still leave the details of the
remote call in the model, but just call it from the controller.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: observe_form should only react to changes in the form

2008-11-10 Thread Mark Reginald James

morgler wrote:
> I have a problem with observe_form:
> 
> <%= observe_form( "search_form",
>   :frequency => 0.25,
>   :update => "search",
>   :loading => "Element.show('spinner')",
>   :complete => "Element.hide('spinner')",
>   :url => { :action => 'update_search' }) %>
> 
> In my form I have a combo box. When the user clicks the combo box and
> scans through the options (WITHOUT yet selecting one), the controller
> action gets called by observe_form. This is pretty annoying. What I
> intend is that the action is only called once the user really clicks
> on an option (thus changing the "model data" of the form if you want).
> How can I only react to "real" changes of the form and ignore
> everything else?

You should remove the :frequency option.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: observe_field with select throwing javascript error

2008-11-05 Thread Mark Reginald James

Corey Murphy wrote:
> Using the following simple form with a select box, the observe_field is
> throwing a javascript error related to the "getValue()" method in the
> prototype library.  Any ideas as to why?  Everything that I can tell is
> well formed syntactically.  I'm running Rails 2.1.0.
> 
> <% form_tag :action => :new do %>
>   <%= select_tag 'transaction_type', "Tuition
> ReimbursementSchools & Seminars" %> />
> 
>   <%= observe_field('transaction_type',
>:url => { :action => "display_alternate_fields" },
>:update => "transaction_specific",
>:with => "'transaction_type=' + encodeURIComponent(value)")
> %>
> 
>    
>   <%= submit_tag "Create" %>
> <% end %>

The problem is that "value" is not defined in this context.

You should be able to just eliminate the :with option.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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: Setting "From" in Mailer model

2008-11-04 Thread Mark Reginald James

Luke Grimstrup wrote:
> Whoops, by meaningful I mean, appearing in my inbox as coming from 
> "MyDomain.com Mailer" instead of just "mailer"
> 
> Luke Grimstrup wrote:
>> At the moment mail sent from my app comes from "mailer" when received by
>> gmail. Is it possible (i.e. some particular format) to make this more
>> meaningful?
>>
>> What I have at the moment one of my typical mailer methods:
>>
>> def updated
>>  recipients  users.collect(&:email).join(',')
>>  from"[EMAIL PROTECTED]"
>>  subject some_subject
>>  body
>> end

You'd write:

from 'MyDomain.com Mailer <[EMAIL PROTECTED]>'

If the email sender is common to all mailer methods
you can move this "from" call to the initialize method,
so it only has to appear once.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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-04 Thread Mark Reginald James

Michael Kahle wrote:

> 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.

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.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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
-~--~~~~--~~--~--~---



  1   2   >