Is model a reserved keyword in rails? - ruby-on-rails

Does anyone know if defining a rails model called "model" can be an issue?
The link that was given in a previous question seems to be broken (http://wiki.rubyonrails.org/rails/pages/ReservedWords)
Thanks!

I found these links:
http://oldwiki.rubyonrails.org/rails/pages/ReservedWords
http://www.yup.com/articles/2007/01/31/no-reservations-about-keywords-in-ruby-on-rails
And some others that weren't as good, and none of them state Model is a keyword.
I also just tried it out. All I did was make a scaffold and then tried out actions on the website, and everything went fine.

Related

rails with mongoid - is there an analog to .loaded?

In a rails / mysql app, there is a method that lets you check if an associated object has been loaded, or if your referencing it will trigger a database query:
a = Author.first
b.books.loaded?
But in mongoid, there is no such method. Is there another method in mongoid that does something similar?
Thanks for any help.
The method is
_loaded?
For some reason SO demands I add more filler, even though the above is the correct answer, so here you go. Here's the opening line from The Great Gatsby, which I love:
In my younger and more vulnerable years my father gave me some advice
that I’ve been turning over in my mind ever since. “Whenever you
feel like criticizing any one,” he told me, “just remember that
all the people in this world haven’t had the advantages that you’ve had.”

How do I make a POST request from one rails app to another?

I have 2 ruby on rails projects, one that has a form and posts it to the another project. The another project should then convert it to a model, quotation, and save it.
Ive made a quotation resource and a controller with a create method. The form's action is other-project/quotations and method post. From what I understand this should call the create method in the quotation controller.
However I am getting this error:
The change you wanted was rejected.
Maybe you tried to change something you didn't have access to.
Anyone know what is causing this error? Is it heroku-caused or does it have something to do with my applications? I'm fairly new to rails/heroku so I'm unsure, any help would be appreciated.
Edit 1
After some research it seems I need the authenticity tokens to match. However since I have two apps, how is this achieved?
Ok so after checking heroku logs it does seem to be an issue with CSRF. When I commented out the protect from forgery line in both apps it worked fine and I was able to send data between them.
Will need to some research into whether this is sensible thing to do (guessing not). Thanks for the tips everyone.

How to fix the Rails mass assignment issue?

After the big news yesterday, I've been trying to find a solid article about how to fix this issue with regard to different versions of Rails, and I'm unable to do so.
The best resource that I have found so far is https://gist.github.com/1978249#file_securing_rails_updates.md, but it only provides one solution: adding ActiveRecord::Base.send(:attr_accessible, nil) to the initializer. This is also the solution presented here at http://railspikes.com/2008/9/22/is-your-rails-application-safe-from-mass-assignment at a much earlier time.
However, at the same time, I remember seeing at another place that just turn on the configuration: config.active_record.whitelist_attributes = true should be suffice.
I am thoroughly confused, from all these different resources, I'm in need to decide between two solutions that doesn't have any reference to which versions of Rails they apply to.
Perhaps I had missed a generic article on the fix after the incident, but I had not found a single article on the rails blog that shows this. I was not able to find it elsewhere, could someone please enlighten me on this. Thanks!
I found this in the gist https://gist.github.com/1978249
Add the following initializer:
config/initializers/disable_mass_assignment.rb
ActiveRecord::Base.send(:attr_accessible, nil)
Looks like a temporary fix to me until rails core comes up with something better !!

Bad Controller/Model names in Rails?

On my current RoR project I have a table that stores pdf's (using paperclip gem). I've named the model 'Document' and a controller called 'documents'. This gave me a lot of weird errors, and I think it's because Rails uses those names already somewhere. From there I wondered if there's a list somewhere with names you should avoid in Rails? Does anybody know if there's something like this on the web? My searches didn't pan out... Also, let me know if you think the name of the model/controller is not the problem at all, so I can do some other checks. Thanx in advance!
The document word is probably a reserved one.
I found a long list of other reserved words here,
There's an attempt at reviving the old Rails wiki reserved words page here:
https://github.com/walterdavis/railsready/wiki

Can't use "acts_as_url" in ruby on rails 3

I'm newb and I'm sorry because of my dumb question! Please, help me!!
I'm working with Rails 3, and this my problem:
I have a model name: Photo using gem 'mongoid'.
I want to make a permalinks which are readable url instead of unreadable '_id' generated from mongoid!
After searching in google, I found a gem called 'stringex'! I decided to use this gem, and put this line in my Gemfile:
gem 'stringex'
then ran "bundle install' to use it.
I just do everything following the guide in github Readme_rsl/stringex but the trouble occurs:
undefined method `acts_as_url' for Photo:Class
Is it because ROR 3 doesn't support this gem? Or I missed something?
Please, I need help!
This is probably not the answer you're looking for, but I think the answer is "you can't do that." At least not currently.
The gem you mention is intimately tied to ActiveRecord (see this issue ticket). If you look in the stringex source code in lib/stringex.rb, you can see that acts_as_url is only included on ActiveRecord and not on Mongoid.
What's happening is that you're using Mongoid on your model, and the nice acts_as_url methods are not attached to the Mongoid::Document. It MAY be as simple as just modifying lib/stringex.rb to also include acts_as_url on Mongoid, though I assume that if it were that simple it would already have been done.
So where does that leave you? There are other ways to generate slugs. I haven't used any of them, so I can't speak to which ones are good or not. Googling "mongoid slug generation" can hopefully point you in the right direction.
Your model needs to have a url attribute to store the nice url in the database for later use.
I don't know why the don't mention that on the README. I found that out after reading a couple of articles.
In my case restarting the rails server command helped.
This isn't difficult to write yourself. And it will help you to understand a few parts of rails and routes.
Add a 'permalink' column to the db.
Do a find_by_permalink(params[:id]) in the controller
Add a def to_param method to the model and return the permalink
column
Add a create_permalink before_validations method. Generate the
permalink however you like and store it in the db.
The only trick is ensuring uniqueness and handling name (and url) changes. stringex looks like it helps with uniqueness, not with name changes.
There are other gems to help with name changes (aliasing and redirecting if there's a new name) if you care about that. You can handle that in different ways.

Resources