What are advantages of the already created endless page plugin?
1)You don't need to install will_paginate or other page based long list implementation
2)You can configure your settings for each endless page independently. For example, you can set offset of 300px from bottom for one page and 100px for other if you want
3)You will have all your configuration in one place instead of set of 'magic integers' inside JS part of application
4)Workable in the IE, Opera and FF.
@all: we are living on the open source world. If you see something you don't like in the existing variant then just let me know and we can improve/fix it.
@Tom, excellent questions. As mentioned at the end of the episode, I think subscribing to the Rails Core google group is the best way to get started. Keep up with the tickets coming in, try them out, and give feedback. Sometimes there will be tickets which don't have a patch, or just include a failing test. These you can try contributing a patch to.
As for coming up with patches from scratch, that's kind of hard to do on the spot. The best thing to do is to work on a Rails application and look for possible patches that will aid your app. This will lead to patches which have a real world use case and will more likely be useful to others.
As for your second question, I've ran into that a number of times, where I thought of making a patch for something and, after a little digging, realized it was already implemented in Rails.
Usually you can tell if it's already implemented by reading the Rails source code. If you don't see it, go ahead and submit a patch and others will point you to the existing functionality if it's already implemented. If it's a larger patch, it's probably best to ask in the google group before attempting to implement it, to see if others would find it useful and if they have any ideas on the implementation.
@Mike, you would still need to call "conditions!" on a scope object. In your code it's just being called on "self" which is the search model.
@Scott, and everyone fighting the checkbox problem with dup fields. I fixed your code. The else if in the javascript didnt run right and I found it more suitable to remove the name field of the hidden field rather than attempt to toggle its value--which did not seem to work.
My solution also allows you to still use check_box instead of check_box_tag. The only mod to the form is to add :onchange => "toggle_name(this);" to the checkbox form element. Then simply declare the toggle_name function in your appplication.js file as such:
how could you make this OR instead of AND. For example I want to search some products and I want to search that my keyword is in the name OR the description OR the manufacture OR the distributor etc..
I just made one big find statement, and that's working for me... but I'd like to make it OR OR OR and hten have the ands at the end so that I could to the above, and also price below X amount, etc..
On my website there is a page to view standings for a so called soccer pool. There are member records with a name per pool, that can be left empty. Each member is linked to a user record with a name field as well. In case the name of the member is empty, the user's name is used in the standings.
When using named_scope, I see strange behavior, since the eager loading isn't quite as I expected. I guess things have been changed in Rails 2.1 in this area that have not necessarily have to do with named_scope.
Anyway, in my Member class this scope doesn't work:
The query doesn't join with the users table, so a missing column error is returned. But when I add :conditions => 'users.id = users.id', it works! So is Rails looking in the conditions to see if a associated table is used?
Anybody see my points and have some clues/comments?
Hello, I was doing a render layout thing, I found that for example if I put the render layout before any @abc = Abc.find(:all), this instance variable line won't work.
So failed is :
def translate
render :layout =>'app'
@pinyin = Pinyin.find(:all)
end
But succeeded is:
def translate
@pinyin = Pinyin.find(:all)
render :layout =>'app'
end
Thanks, Walter. You apparently have a different concept of "flaming" than I do, but you were right about missing javascript_include_tag and I appreciate your taking the time to provide the answer.
Ryan also provided a nice description of this technique in Mike Clark's new book, Advanced Rails Recipes.
(There is not much room in this comment window). The module code would have this:
scope = klass.scoped({})
non_blanks = scopes.keys.select { |attr| !send(attr).blank? }
...
You can then use inject or a simple each to build up the scope inside of a loop over 'non_blanks'. To support the 'LIKE' statement, it would need to support lambda.
Very cool, Ryan. You sure covered a lot of territory here ... kind of like one of those cooking shows where all the vegetables and ingredients are prepared in advance, and the food has already baked in advance :-)
So this gets to the core of the main steps of making a contribution. For the more timid amongst us, do you have any pointers on some of the "pre-show" work that should go into making a patch?
For example, you mentioned in passing that it was a good idea to make sure the functionality wasn't already implemented. Now being about 9 months into RoR I am still learning a lot of things I just didn't know existed. Any tips on knowing what a good candidate is for a patch?
I know I'm asking a question long after the cast was posted, but I'm been having trouble with this for a while.
When I include the AuthenticatedSystem module in the Application controller, the function "logged_in?" works correctly, but whenever I try any other methods such as "authorized?", Rails throws an error saying it's not defined?
If your Product model "has_many :colors" how would you also add the ability to to return only products that come in a certain color - assuming you have a text field in your search form named "color" and the Color model has a field named "name"?
I am still a little bit confused on how I can give each of the checkboxes a unique id attribute. I'd like to create a label for each of my checkboxes which I am currently unable to do without unique ids.
The excerpt from the book on this topic was insanely helpful. Thanks for all the good work on this and other screencasts and reference materials. I've learned more about Rails from you than any other single source, hands down.
I've adopted a similar approach but using a MultiCondition object (http://github.com/weppos/activerecord-multiconditions/).
The main advantage is that I can queue illimitate conditions and I can decide which conditions should be queued for which methods.
Using a solution based on method names might cause some trouble when I would use, for instance, all conditions in action A and all conditions - 1 in action B.
Will be interesting to see, if future episodes included testing as part of the vidoe..
I know there is bunch of condition builders out there, wouldn't it cool if your named_scope tutorials end up being a simple condition builder implemented on rails 2.1
Ok, I figured it out, but not sure why it is...yet. Turns out my example _was_ slightly different; I was using a table - instead of divs - to format the form.
As soon as I removed the table, it worked.
I'll have to dig into the source code in order to figure it out.
I know that you're not Rails tech support, but wondering how much this has changed with the release of Rails 2.1.
I have a clean 2.1 demo site - using cookie store - with basically the same set up as your screencast. The HTML POST form works fine, but when I switch to form_remote_for in my view file, I get an 'ActionController::InvalidAuthenticityToken' error. This only occurs when using remote. I do have a responds_to for js, and a corresponding create.js.erb, but it appears that the action never gets called. The request dies before it gets there due to the authenticity error.
If I comment out protect_from_forgery in application.rb, I receive a 200 response, but it appears that the request has a content length of 0.
I know I'm missing something simple. I'll keep digging, but wondering if you've you run across this in your travels?
For those of you pulling your hair out with this complicated solution, there is an excellent pluin that handles all this fun stuff in just a few lines of helper code.
"Check it out!":http://dvisionfactory.googlecode.com/svn/rails/plugins/related_select_forms/
I am having the same exact problem as Captain Zhan. This script totally borks out on the edit action. It forgets the state I selected even though I see it clearly saved in the database.
All of my javascripts are definitely included and the script still works - if I change the country, the state list changes, but the initial load is screwed up and automatically selects the first state in the list, instead of the state saved in the DB.
As a temporary solution, I simply do not load the script i current action is edit, but thats not too cool of a fix.
Update: in the ActiveResource code, all the find methods are calling the private method instantiate_record, which essentially just does a Base.new(hash_returned_from_get). There's also a similar private method instantiate_collection.
Unfortunately, these are private methods - it would be great if they were protected, so we could call them from our subclasses. In the meantime, I just use the dirty dirty workaround:
def get_record(path)
MyActiveResource.new(get(path))
end
I second Chris's question. It seems like if you do MyActiveResource.find(1).get(:custom_method), ActiveResource returns a hash instead of object that you can call with methods.
Rails newbie here. Thanks so much! Your railscasts are amazing. What should my partial page look like? I can't seem to get the partial to display my search results correctly.
Thanks for this update. I would like to point out an issue that arises when you freeze the current date for "today" in your application.
In general, the new version setting is fine because I doubt anyone was previously up to version 20080605122345. This is NOT ok if you're setting the time and date of "right now" in the application. For example, the date of present time is set to midnight, May 15th, 2007 in an app i'm working on (obviously development only). This means my new migrations (ALL of them) are given the same version of 20070515000000_.... and this is bad news.
I just manually rename them and all is well. An untrained developer picking up someone else's project might not notice this right away.
And it almost works, but the flaw is obvious. If the attribute is left blank, it wont check be changed. I can run validations using this on almost anything except for presence_of is there any way around this?
Thanks for your help.
Hi Ryan.
Small question and answers.
What are advantages of the already created endless page plugin?
1)You don't need to install will_paginate or other page based long list implementation
2)You can configure your settings for each endless page independently. For example, you can set offset of 300px from bottom for one page and 100px for other if you want
3)You will have all your configuration in one place instead of set of 'magic integers' inside JS part of application
4)Workable in the IE, Opera and FF.
@all: we are living on the open source world. If you see something you don't like in the existing variant then just let me know and we can improve/fix it.
Nice implementation. I'll definitly be using this in the near future. Thanks
Hi Ryan,
I am always been a great fan of your railscast. Keep up the good work.
Hi!
in the page.insert_html line, the Task.new call is used as parameter.
Where the Task object comes from?
Thanks,
--
Guerra
@Tom, excellent questions. As mentioned at the end of the episode, I think subscribing to the Rails Core google group is the best way to get started. Keep up with the tickets coming in, try them out, and give feedback. Sometimes there will be tickets which don't have a patch, or just include a failing test. These you can try contributing a patch to.
As for coming up with patches from scratch, that's kind of hard to do on the spot. The best thing to do is to work on a Rails application and look for possible patches that will aid your app. This will lead to patches which have a real world use case and will more likely be useful to others.
As for your second question, I've ran into that a number of times, where I thought of making a patch for something and, after a little digging, realized it was already implemented in Rails.
Usually you can tell if it's already implemented by reading the Rails source code. If you don't see it, go ahead and submit a patch and others will point you to the existing functionality if it's already implemented. If it's a larger patch, it's probably best to ask in the google group before attempting to implement it, to see if others would find it useful and if they have any ideas on the implementation.
@Mike, you would still need to call "conditions!" on a scope object. In your code it's just being called on "self" which is the search model.
Of course, the necessity of being able to administrate a broken site is debatable.... Also, why would you publish your broken site?
The code I posted does not appear to work properly under IE due to its nextSibling implementation. I'll try to find a more robust solution on monday.
@Scott, and everyone fighting the checkbox problem with dup fields. I fixed your code. The else if in the javascript didnt run right and I found it more suitable to remove the name field of the hidden field rather than attempt to toggle its value--which did not seem to work.
My solution also allows you to still use check_box instead of check_box_tag. The only mod to the form is to add :onchange => "toggle_name(this);" to the checkbox form element. Then simply declare the toggle_name function in your appplication.js file as such:
http://pastie.org/214828
never , i figures it out, its working now on the console.as a newbie i forgot to chnage envrinment.rb
thx
Hi
im getting this:
named_scope > NoMethodError
ruby-v
ruby 1.8.5 (2006-08-25) [i486-linux]
rails-v
Rails 2.1.0
gem1.8 list
*** LOCAL GEMS ***
actionmailer (2.1.0, 2.0.2)
actionpack (2.1.0, 2.0.2)
activerecord (2.1.0, 2.0.2)
activeresource (2.1.0, 2.0.2)
activesupport (2.1.0, 2.0.2)
jruby-openssl (0.2.3)
rails (2.1.0, 2.0.2)
rake (0.8.1)
my problem / question: as soon as i have a named_scope declaration within the class, im experiencing the error above ( NoMethodError)
what am i doing wrong/missing?
thx tom
Hey Ryan,
how could you make this OR instead of AND. For example I want to search some products and I want to search that my keyword is in the name OR the description OR the manufacture OR the distributor etc..
I just made one big find statement, and that's working for me... but I'd like to make it OR OR OR and hten have the ands at the end so that I could to the above, and also price below X amount, etc..
Thanks in advance if you have a chance.
On my website there is a page to view standings for a so called soccer pool. There are member records with a name per pool, that can be left empty. Each member is linked to a user record with a name field as well. In case the name of the member is empty, the user's name is used in the standings.
When using named_scope, I see strange behavior, since the eager loading isn't quite as I expected. I guess things have been changed in Rails 2.1 in this area that have not necessarily have to do with named_scope.
Anyway, in my Member class this scope doesn't work:
named_scope :standings, :include => :user, :order => 'members.position, COALESCE(members.name, users.name)'
The query doesn't join with the users table, so a missing column error is returned. But when I add :conditions => 'users.id = users.id', it works! So is Rails looking in the conditions to see if a associated table is used?
Anybody see my points and have some clues/comments?
@Harry J The plugin you mentioned works great!
Having said that, I also think the screencast is excellent, because it is all about learning and seeing new or 'unknown' techniques.
Hello, I was doing a render layout thing, I found that for example if I put the render layout before any @abc = Abc.find(:all), this instance variable line won't work.
So failed is :
def translate
render :layout =>'app'
@pinyin = Pinyin.find(:all)
end
But succeeded is:
def translate
@pinyin = Pinyin.find(:all)
render :layout =>'app'
end
Does anyone know why this is?
Thanks a million!
Thanks, Walter. You apparently have a different concept of "flaming" than I do, but you were right about missing javascript_include_tag and I appreciate your taking the time to provide the answer.
Ryan also provided a nice description of this technique in Mike Clark's new book, Advanced Rails Recipes.
-- Mike
Tanks for this screencast!
Nice walk through on contributing. With the patch you wrote couldn't you have re-written your method as:
def find_products
returning Product.scoped({}) do |conditions|
conditions! ...
conditions! ...
conditions! ...
conditions! ...
end
end
I was searching on the web for a way of expressing a one_to_many relation in fixtures, and found it only in your site.
Thanks for your work Ryan!
One way to remove the duplication is by making a mixin module that you call like this inside of ProductSearch:
searches Product, {
:minimum_price => "products.price >= ?",
:maximum_price => "maximum_price <= ?",
:category_id => "products.category_id = ?"
}
(There is not much room in this comment window). The module code would have this:
scope = klass.scoped({})
non_blanks = scopes.keys.select { |attr| !send(attr).blank? }
...
You can then use inject or a simple each to build up the scope inside of a loop over 'non_blanks'. To support the 'LIKE' statement, it would need to support lambda.
Daniel S:
Namespaced models do not seem to support the new 'named' fixtures. I just ran into this problem.
I have found that as of Rails 2.1, you need to require 'environment' to be able to use models in a rake task
I figured it out. You need to add in an ":include" to the "find" like so:
def find_products
Product.find(:all, :conditions => conditions, :include => :colors)
end
and then you use:
def color_conditions
["colors.name = LIKE", "#{color}"] unless color.blank?
end
Very cool, Ryan. You sure covered a lot of territory here ... kind of like one of those cooking shows where all the vegetables and ingredients are prepared in advance, and the food has already baked in advance :-)
So this gets to the core of the main steps of making a contribution. For the more timid amongst us, do you have any pointers on some of the "pre-show" work that should go into making a patch?
For example, you mentioned in passing that it was a good idea to make sure the functionality wasn't already implemented. Now being about 9 months into RoR I am still learning a lot of things I just didn't know existed. Any tips on knowing what a good candidate is for a patch?
Thanks for all you do.
Tom
Very Good!
tnks!
nice & easy for newbie like me :)
thanks :D
Hi Ryan,
Thanks for your great rails screencasts.
I've small tip for the people who use "vim" as code editor. If you want to exclude ".swp" files, just add "*.swp" to your ".git/info/exclude" file.
I know I'm asking a question long after the cast was posted, but I'm been having trouble with this for a while.
When I include the AuthenticatedSystem module in the Application controller, the function "logged_in?" works correctly, but whenever I try any other methods such as "authorized?", Rails throws an error saying it's not defined?
Any ideas?
Nick
Now people wont one less excuse for not contributing back to rails :)
If your Product model "has_many :colors" how would you also add the ability to to return only products that come in a certain color - assuming you have a text field in your search form named "color" and the Color model has a field named "name"?
For Windows Users:
Download and install Git program.
http://code.google.com/p/msysgit/
Then follow run commands above.
I am still a little bit confused on how I can give each of the checkboxes a unique id attribute. I'd like to create a label for each of my checkboxes which I am currently unable to do without unique ids.
Any help will be most appreciated.
The excerpt from the book on this topic was insanely helpful. Thanks for all the good work on this and other screencasts and reference materials. I've learned more about Rails from you than any other single source, hands down.
Great job Ryan,
I never thought I will saw such a beautiful code for multiconditional search.
That's an amazing solution! :)
I've adopted a similar approach but using a MultiCondition object (http://github.com/weppos/activerecord-multiconditions/).
The main advantage is that I can queue illimitate conditions and I can decide which conditions should be queued for which methods.
Using a solution based on method names might cause some trouble when I would use, for instance, all conditions in action A and all conditions - 1 in action B.
Very interesting.. Thanks again ryan..
Will be interesting to see, if future episodes included testing as part of the vidoe..
I know there is bunch of condition builders out there, wouldn't it cool if your named_scope tutorials end up being a simple condition builder implemented on rails 2.1
Awesome topic, thanks Ryan :)
Is there a way to get the exception_notifier plugin to use the filter_parameter_logging directive?
Anyone played with exception_notifier and parameter logging?
Ok, I figured it out, but not sure why it is...yet. Turns out my example _was_ slightly different; I was using a table - instead of divs - to format the form.
As soon as I removed the table, it worked.
I'll have to dig into the source code in order to figure it out.
Thanks for listening. ;-)
Ryan,
I know that you're not Rails tech support, but wondering how much this has changed with the release of Rails 2.1.
I have a clean 2.1 demo site - using cookie store - with basically the same set up as your screencast. The HTML POST form works fine, but when I switch to form_remote_for in my view file, I get an 'ActionController::InvalidAuthenticityToken' error. This only occurs when using remote. I do have a responds_to for js, and a corresponding create.js.erb, but it appears that the action never gets called. The request dies before it gets there due to the authenticity error.
If I comment out protect_from_forgery in application.rb, I receive a 200 response, but it appears that the request has a content length of 0.
I know I'm missing something simple. I'll keep digging, but wondering if you've you run across this in your travels?
For those of you pulling your hair out with this complicated solution, there is an excellent pluin that handles all this fun stuff in just a few lines of helper code.
"Check it out!":http://dvisionfactory.googlecode.com/svn/rails/plugins/related_select_forms/
It also works well on edit!
I am having the same exact problem as Captain Zhan. This script totally borks out on the edit action. It forgets the state I selected even though I see it clearly saved in the database.
All of my javascripts are definitely included and the script still works - if I change the country, the state list changes, but the initial load is screwed up and automatically selects the first state in the list, instead of the state saved in the DB.
As a temporary solution, I simply do not load the script i current action is edit, but thats not too cool of a fix.
Anyone else deal with this?
Where is Ryan???? *shines Bat Signal*
thank you very much! I have successfully used it. This is a very important thing for me :)
Update: in the ActiveResource code, all the find methods are calling the private method instantiate_record, which essentially just does a Base.new(hash_returned_from_get). There's also a similar private method instantiate_collection.
Unfortunately, these are private methods - it would be great if they were protected, so we could call them from our subclasses. In the meantime, I just use the dirty dirty workaround:
def get_record(path)
MyActiveResource.new(get(path))
end
Calling it with
MyActiveResource.find(1). get_record(:custom_method)
I second Chris's question. It seems like if you do MyActiveResource.find(1).get(:custom_method), ActiveResource returns a hash instead of object that you can call with methods.
What the best way to do something like this:
Task.find(:all, :conditions => {:start.year => 2008})
without user database functions...
It's posssible?
Very good Rails screencasts!
Thank's
Nevermind...I got the search results working! Just forgot my for loop!
Thanks for the cast!
Rails newbie here. Thanks so much! Your railscasts are amazing. What should my partial page look like? I can't seem to get the partial to display my search results correctly.
Thanks for this update. I would like to point out an issue that arises when you freeze the current date for "today" in your application.
In general, the new version setting is fine because I doubt anyone was previously up to version 20080605122345. This is NOT ok if you're setting the time and date of "right now" in the application. For example, the date of present time is set to midnight, May 15th, 2007 in an app i'm working on (obviously development only). This means my new migrations (ALL of them) are given the same version of 20070515000000_.... and this is bad news.
I just manually rename them and all is well. An untrained developer picking up someone else's project might not notice this right away.
@Matt, since we're using scopes here I *think* you can just call ".paginate" on the resulting scope and it will just work. I haven't tested this yet.
@Ben, good point. Testing is something I hope to cover more in future episodes.
Hi Ryan-
I can do something like
validates_presence_of :p1_new_first_name, :p1_new_last_name, :if => :p1_new_first_name_changed?
validates_presence_of :p1_first_name, :p1_last_name, :if => :p1_first_name_changed?
And it almost works, but the flaw is obvious. If the attribute is left blank, it wont check be changed. I can run validations using this on almost anything except for presence_of is there any way around this?
Thanks for your help.