I'm starting in the big wooly world of Ruby on Rails and i'm trying to get my head around scaffolds and models. (cue, I'm a designer)
I use the rails generate scaffold command
rails generate scaffold Lesson title:string description:text
But is it possible to update the Lesson table with new key, values with rails?
I tried:
rails generate model Lesson title:string description:text dtstart:datetime
But when I run the db:migrate it fails and the only way around I know of to do that is to delete all of the scaffold and regenerate it.
I'm sure there must be an easier solution :)
I think (but im not sure, that it is because of the db/development.sqlite3 file that is not updated, when I delete the content by hand it then run the bd:migrate) is there a way to have evrything updated at once?
I don't think there is a way to do what you describe - generally if you want to add new fields to a model, you want to generate a migration:
rails g migration AddStartToLesson
Then open the migration file and add the code that will add those fields. It will probably end up looking something like this:
class AddStartToLesson < ActiveRecord::Migration
def self.up
add_column :lessons, :start, :datetime
end
def self.down
remove_column :lessons, :start
end
end
And you'll have to update some of the views - probably _form.html.erb, to get the form field to enter that data, and index.html.erb and show.html.erb to display it. (they're probably in app/views/lessons/)
I think you must also update (in rails 5 at least) your lesson_params in lessons_controller.rb in order to allow the new parameters to be passed from the view to the model.
Related
I am using Cakedc Users plugin and I want to add a small entry to the Users table which is "Balance" (integer)
I read the extending part in the documentation and I honestly got dizzy from all the modifications that I have to do
I don't want to rewrite the whole thing just for a small entry, Is there anyway I can add it to the table with minumum modification or rename another entry that I don't need, like "tos_date" or something
Well, let me try to help about how extending the model should be:
Modify the users table (via migrations or manually) and add the columns you want.
Copy the template files with the forms, from the plugin itself to your app under the folder src/Template/Plugin/CakeDC/Users/name_of_the_controller/name_of_the_action, then modify the forms to add a new control for your custom column
You're done
Thanks!
I'm looking for the best/most efficient way to add categories that save to a database. I've created the category model with a name column. I generated the controller. My first thought was to just enter the categories directly into the controller since I will be the only one that can create, edit, and destroy them. This is what it looked like:
class CategoriesController < ApplicationController
def women
end
def kids
end
def babies
end
def home_decor
end
end
Fine, but that doesn't save to the db and since I'd like to associate these categories w/ products later I need it to save to the db. I could create these categories directly in console, but I'm not sure if when I push to production (on Heroku) how to create them again (and that seems a little tedious).
The other option I have is to create a form to create the categories and only give access to an admin.
Am I missing something? Is there a more efficient way to do this or is the admin route the best way to handle it?
Thanks for any suggestions!
Forget that controller. What you are looking for is in db/seeds.rb. It's just a script where you place the default content of your database. So in your case you'll need to put the following:
Category.delete_all # For avoiding duplicate content
Category.create!({id: 1, name: 'women'}) # Use create! so you'll know if there is any errors
Category.create!({id: 2, name: 'kids'})
# etc...
Then just run the script for placing that content in the db:
rake db:seed
And in heroku:
heroku run rake db:seed
I am still not very familiar with Symfony 1.4 and a few things are still unclear to me.
What I have done:
I have recently added a new table to my Symfony 1.4 project's database (let's call it "A")
I have regenerated the schema.yml file using $ php symfony doctrine:build-schema.
What I would like to do:
When submitting the "add" form (in the /new/ page) of another, different module, inserting a row in the "A" table.
-
In the future, I will need to fetch data from "A" and display them in the backend part of the website.
I don't have a backend module related to that new table. (Should I? How?)
If you want to do anything when you add a row to a table (let's call it B to distinguish from your A) you can use doctrine's lifecycle hooks, the pres and posts. In your case it could be a postInsert. (you can also use Save, Update, Delete, each with pre or post).
Each of this functions is executed in a given moment (before or after an event) of a given event. The Save hook is executed after any inserts or updates so you don't have to copy functionality to two functions.
You should implement the method in your B class and add a row to A.
To answer your other question - it's best if you use the Symfony's admin generator and create a module for A. You will have a list of all the rows, possibility to filter the list and to modify the rows.
I am pretty new to Django and just got a job that involves maintaining and adding features to a site I did not design, so I am still kind of confused about the structure and what not of the project. The site is using South for database migrations and I've got a hang of using it to add new applications to the project. The trouble I am having now is that I need to delete a certain field in a model because it is no longer needed and on the admin page it is required to be filled out. From my understanding of Django so far it appears to be a custom field. It is defined like this in its own separate library application(still not sure if thats the right lingo).
class Genre(models.Model):
name = models.CharField(max_length=255)
def __unicode__(self):
return u"%s" % self.name
Here is the models that uses the custom field if that helps out any.
class Entry(models.Model):
artist = d51fields.ForeignKey(Artist, instantiate_fn=instant_artist)
album = d51fields.ForeignKey(Album, js_methods=['match_artist_and_startswith'], instantiate_fn=instant_album)
track = d51fields.ForeignKey(Track, js_methods=['match_album_and_startswith'], instantiate_fn=instant_track)
genre = models.ForeignKey(Genre)
submitted = models.DateTimeField(auto_now_add=True)
is_rotation = models.BooleanField()
dj = models.ForeignKey(DJ)
show = models.ForeignKey(Show, null=True, blank=True)
objects = EntryManager()
def __unicode__(self):
return "%s [%s]" % (self.artist, self.track)
class Meta:
verbose_name = "entry"
verbose_name_plural = "entries"
I've looked at the documentation for migrating custom fields but it is all really confusing for me, so I am looking for some more help. I just want to get rid of the table holding the Genre field and clean up the dependencies with the foreign keys associated with it. Do you think I should write some custom rules for South and use a migration or just try and do it manually in Postgresql. I tried doing it with just Postgres and I failed miserably.
Any guidance would be greatly appreciated. If you want more info about the situation just ask and I can add it to the post. I have a feeling there is a lot of dependencies I will have to deal with, but hopefully there is a simple fix.
Also if some one knows how to get a good view of the database structure that would be great.
Thanks so much. All of you guys are great.
Edit1
Here what I got when I removed the ForeignKeys and then ran
manage.py schemamigration logs --auto
! Cannot freeze field 'logs.entry.artist'
! (this field has class d51_admin_autofk.fields.ForeignKey)
! Cannot freeze field 'logs.entry.album'
! (this field has class d51_admin_autofk.fields.ForeignKey)
! Cannot freeze field 'logs.entry.track'
! (this field has class d51_admin_autofk.fields.ForeignKey)
! South cannot introspect some fields; this is probably because they are custom
! fields. If they worked in 0.6 or below, this is because we have removed the
! models parser (it often broke things).
I am not totally sure what sort of action I should take next. I looked into the South documentation and it wasn't too clear about how to write the rules for migrating things like this.
I don't see any custom field anywhere in the code you posted. All I see is two models, all containing standard fields shipped with Django.
If I understand correctly, you can just delete all ForeignKey references to your Genre model. Then run ./manage.py schemamigration <yourappname> --auto. This will ask you for a default value for the genre field in the Entry model, provide an ID of some kind. (This is because migrations can be applied both forwards and backwards, so if you try to undo the migration, this is the value that will get inserted in all your model instances.)
Finally, just applying the migration should make it happen: ./manage.py migrate <yourappname>.
After that you should be safe to drop the table storing your Genre model.
Be sure to try this on a development server though, just to make sure it doesn't blow up. (-;
I developed small CakePHP application, and now I want to add one more table (in fact, model/controller/view) into system, named notes. I had already created a table of course.
But when I run command cake bake model, I do not get table Notes on the list. I can add it manually, but after that I get some errors when running cake bake controller and cake bake view.
Can you give me some clue why I have those problems, and how to add that new model?
I would also check your app/config/database.php to ensure that you are using the correct database configuration. You may well have added the table to a different database perhaps and the bake is picking up the other database. Also, and this may be obvious, but check you are in the right project, it's easy to be in a different folder and not realise, especially if you have lots of projects.
I'm not aware of a limit on the bake listing. I would check your database to make sure the table exists and has some columns. You can always open up the console bake script and check for a limit and increase it if needs be.
I found solution!
I had to delete all from cache directory, /app/tmp/cache/models
Now it works!
:-)
When you say added it manually, do you mean added the note.php model? If not, you may want to try that. Verify that the model name is correct for the following:
file name: note.php
class name: class Note extends AppModel
table name: notes
Also, be sure the notes table has the id column and it is set to primary key.
If this does not push you in the right direction, please post your notes table schema here. Also, have you had success in baking other things in your app? Have you upgraded anything?
Please change the following farameters to bake:
For Controller:
/cake/console/libs/tasks/controller.php
function listAll($useDbConfig = 'default') {
change to :
function listAll($useDbConfig = 'YOUR DB CONFIG NAME') {
NOW DO CAKE BAKE for CONTROLLER! ENJOY!