rails send multiple commands at once - ruby-on-rails-3

How can you create multiple scaffolds at once at the rails command line?
so that these commands could be send at once
rails g scaffold person name:string
rails g scaffold adress street:string person:references
rails g scaffold website url:string person:references
Thank you

Generators are initialized from scripts/rails. You could specify your own generator which just chains generators, or use something like rake to invoke multiple commands, or use a shell script if you want them literally chained.

Related

Rails Scaffold based on existing model not working

I want to generate scaffolding based on an existing model using scaffold_controller.
rails generate scaffold_controller NAME
I only get empty views, I'm not getting the fields defined in the migration. Not sure what is really going on because I use to do this and it use to work all the time in previous projects.
I'm using Rails 3.2.12, is this feature no longer available?
Thanks for the help.
If you want to have attributes in your views, you have pass them into the generate scaffold_controller command, like this:
generate scaffold_controller <YOUR_MODEL_NAME> attribute1 attribute2 attribute3
Then it will create views with just those attributes in them.
After further investigation, I think the problem is specific to my Rails installation, not sure what but if you ever get this problem, this is the workaround that I found but requires twitter bootstrap:
rails generate scaffold_controller <YOUR_MODEL_NAME>
rails g bootstrap:themed <YOUR_MODEL_NAME> -f
Hope it helps.
Your example uses scaffold_controller where as if you would like a model to be created and displayed in your views you should use just scaffold. In the case of your code example:
rails generate scaffold_controller NAME
This should become:
rails generate scaffold NAME
You will need to specify your attributes and then run
rake db:migrate

make a request to other site from the rake task

I'm trying to automate my database population with sample objects, so I use rake tasks. I would like to use some data from other website pages.
I created txt file with url list from where that information supposed to be read and ran into problem: I don't know how to make requests outside from rake task. I need to get response, extract some selectord as RSPEC allows to do. Thank you, guys!
For custom rake tasks, you can always create your own plugin. Refer rails guide - plugins section.
In the plugin, you can use get method to get the data from an external URL. Refer ActionDispatch::Integration::RequestHelpers methods for the details.
You can add your own extraction logic and seed the database.

$ rails generate scaffold * <-what's available to go here, and where can i find it?

so, finding tutorials around the internet provides some of the different types of scaffold available, like:
$ rails generate scaffold Post
$ rails generate scaffold Club
(Answered)1)where can i find a list of the scaffolds available to a standard rails 3 install? possibly with descriptions/screenshots?
(Answered)2)is there a recommended place for community provided scaffolds?
3)what data types can i use for the variables created with scaffolding? for instance, is there a date datatype? float? any ruby datatype?
4)where do i go to find this documentation? i've been searching for info on scaffolding w/o much success, what should i be looking for?
The stuff that goes after scaffold is the name of the model you want to create and optionally its properties. The rails scaffold generator will then create a Model class, Migration, Controller, basic Views and Route entry. So in your two examples it will create scaffolding for a model called Post and another one for a model called Club. You can name these whatever you want e.g.
rails generate scaffold MyModel.
Best resource to start is the getting started guide:
http://guides.rubyonrails.org/getting_started.html

RoR - Foreign Keys created via associations and migrate or "by hand" (or scaffold)?

Just starting to learn Ruby on Rails. I'm using RoR 3. I have read this: http://guides.rubyonrails.org/association_basics.html
But I want to make sure I understand completely.
When creating a new model (I'm doing via scaffold for now), should I specify foreign_key fields at that point, or does the association handle that completely? I believe that association is only at app level, not at the db level, correct?
So I think I must do:
rails generate scaffold post body:text title:string user_id:integer
So in summary, when creating a blog application, must I specify the user_id field in the post model, or does the user model's has_many :posts take care of actually adding that to the db (mine is mysql) when I migrate?
And if the answer is that I should do them when I create the model in the first place (via scaffold or by hand), what happens when I decide later on that I want to add a foreign key, must I add that as an execute statement in a new migration?
You're correct. You need to specify the foreign key when you create your scaffold/model/migration as you stated to get the DB to be correct, and the has_many takes cares of the model for you.
So for initial generation of a scaffold (or model), just do:
rails generate scaffold post body:text title:string user_id:integer
as you stated, and add the has_many for the model itself.
For additions later on, you would make up a new migration, something like (assuming you want to use generation, but you could write your own migration):
rails generate migration add_user_id_to_posts user_id:integer
With that, you can run a rake db:migrate, and then update your model with a has_many or whatever association you need.

How to connect Social Media Streams (Twitter, Instagram) in Rails

I am doing my first baby steps with Rails (version 3) now. I'd like to create a simple app catching data from the twitter/instagram/ API. Thankfully there are already gems doing the heavy lifting with connecting to the services, providing the data. Now I wonder what the best-practice is to add this functionality to Rails correctly.
My feeling is the best way is to create a new non-DB Model for each service I want to include and create some scopes, which I will then use in the controller. The scope definition includes the functional code, instantiating and using the twitter/instagram gems to get the web service's data.
Is this model/scope approach right or did I miss something?
In future I might need to cache all the fetched data to handle common API request limitations. Is there any good approach for this?
I'd appreciate your thoughts or examples/resources on this topic.
I think in this situation a standard class would be ok e.g.
class TwitterImport
def get_tweets
# Download tweets
...
# Create tweets
Tweet.create(.....
end
end
I created something similar recently. I created a folder called scripts in my app directory and stuck a class in there called import.rb. Because this file lives within the app directory it automatically has access to the Rails goodness i.e. existing Models etc.
I then set it up as a rails runner script, you run it via teh console from your app's root directory:
rails runner -e development TwitterImport.get_tweets