I created this table called noticias
class CreateNoticia < ActiveRecord::Migration
def change
create_table :noticias do |t|
t.string :titulo
t.date :data
t.text :sumario
t.text :texto
t.boolean :destaque
t.boolean :ativa
t.timestamps
end
end
end
and of course I created the model
class Noticia < ActiveRecord::Base
#validations
end
and when I try to create a Noticia it throws:
cannot find table 'noticia' so far I've been creating the models with the rails generate model command and it has all worked out. But now this happens. I don't understand.
Does someone know what's happening?
noticia seems to be plural form of noticium, so:
ActiveSupport::Inflector.pluralize('noticia')
# => "noticia"
That's why Rails expect noticia table.
Related
I made 'hospital_review' table and 'hospital_review_comments' table with 1:N relationship on ruby on rails.
'hospital_review' table's migration file is like this.
class CreateHospitalReviews < ActiveRecord::Migration
def change
create_table :hospital_reviews do |t|
t.string :written_time
t.string :writter
t.integer :user_id
t.string :category
t.string :hospital_name
t.text :content
end
end
and 'hospital_review_comments's one is like this.
def change
create_table :hospital_review_comments do |t|
t.integer :user_id
t.integer :post_id
t.string :writter
t.string :written_time
t.text :content
t.timestamps null: false
end
end
'hospital_review' table's model file is like this.
belongs_to :user
has_many :hospital_review_comments
'hospital_review_comments' table's one is like this.
belongs_to :user
belongs_to :hospital_review
I wanted to show each hospital review and the comments that are written on it, so I programmed the codes below in 'show.html.erb'.
<% #post.hospital_review_comments.each do |comment| %>
<p><strong><%=comment.user.username%></strong> <%=comment.content%></p>
and this is show action in controller file.
def show
#post = HospitalReview.find(params[:id])
#hospital_comment_writer = User.where(id: session[:user_id])[0]
end
but the error occcured with message
'SQLite3::SQLException: no such column:'.
I tried 'foregin_key' in hospital_review_comments table's model file, but it didn't work. I can't get the reason the error occurred. Plz help!
You are missing hospital_review_id in your hospital_review_comments table
I think you have wrongly added post_id in hospital_review_comments table, which should be hospital_review_id, that will to the job.
Else you can add the foreign_key option in the association as follows.
has_many :hospital_review_comments, foreign_key: 'post_id'
Hello i have to model like;
class CreateLecturers < ActiveRecord::Migration
def change
create_table :lecturers do |t|
t.string :firstname
t.string :lastname
t.string :position
t.string :email
t.timestamps null: false
end
end
end
and here is my second model.
class CreateCurriculums < ActiveRecord::Migration
def change
create_table :curriculums do |t|
t.string :title
t.integer :hours
t.integer :year
t.text :description
t.timestamps null: false
end
end
end
I want migrate to Curriculums to Lecturer. But not with id, with title
how it's can be possible?
So i use rails-admin. When i add some Curriculum i want to choose with dropdown lecturer and when i add some Lecturer i want to choose Curriculum between models .
No matter what, you have to have association between two models. Also do not forget to add curriculum_id to lectures table.
curriculum.rb
has_many :lectures
lecture.rb
belongs_to :curriculum
To add migration
rails g migration add_curriculum_id_to_lectures curriculum_id:integer
I have this table structure:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :username
t.string :email
t.string :encrypted_password
t.string :salt
t.timestamps
end
end
end
And I want to add a new table as shown below:
class CreateHolidays < ActiveRecord::Migration
def change
create_table :holidays do |t|
t.string :earn_leave
t.string :seek_leave
t.string :unplanned_leave
t.timestamps
t.timestamps
end
add_index(users,id)
end
end
What should I do for this, please also suggest commands that can/should be used for migration.
You want to look up about foreign_keys:
#app/models/holiday.rb
class Holiday < ActiveRecord::Base
belongs_to :user
end
#app/models/user.rb
class User < ActiveRecord::Base
has_many :holidays
end
This will mean you have to add the user_id foreign key to your holidays data table:
class CreateHolidays < ActiveRecord::Migration
def change
create_table :holidays do |t|
t.references :user
t.string :earn_leave
t.string :seek_leave
t.string :unplanned_leave
t.timestamps
t.timestamps
end
end
end
You must remember that Rails is designed to be built on top of a relational database. As such, it uses foreign_keys in the tables to locate associated records:
The above will allow you to call:
#user.holidays
and
#holiday.user
I am creating a gem
I have a generator to create a migration based on a name of your choosing
rails g my_generator MODEL
I am not using rails'
rails g migration XYZ
But instead pretty much copying what a schema would look like...example: if a user types the following
rails g my_generator Item
You get:
class CreateItem < ActiveRecord::Migration
def change
create_table "items", force: true do |t|
t.string "title"
t.integer "color_id"
t.integer "size_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "base_product_id"
t.integer "stock_qty"
end
end
end
The name of the migration is (my_generator)create_items.rb I have not inserted the timestamp in the start of the migration. That is really the only difference between a rails g model Item's migration, and the one I get from my generator.
My migration doesn't do anything, but I tested creating a new model, and running rake db:migrate, the rails g model migrates, mine does not.
Here is my generator:
class Rails::ShiftedCommerceGenerator < Rails::Generators::NamedBase
def create_main_model
create_file "app/models/shifted_commerce/#{plural_name.singularize}.rb", <<-FILE
class #{class_name} < ActiveRecord::Base
belongs_to :base_#{plural_name.singularize}
has_many :line_items
belongs_to :size
belongs_to :color
validates_uniqueness_of :base_#{plural_name.singularize}_id, :scope => [:size_id]
def is_base_#{plural_name.singularize}?
return true if self.class.name == "Base#{class_name}"
end
def is_#{plural_name.singularize}?
return true if self.class.name == "#{class_name}"
end
def in_stock
self.stock_qty > 0
end
end
FILE
end
def create_main_migration
create_file "db/migrate/shifted_commerce_create_#{plural_name}.rb", <<-FILE
class Create#{class_name} < ActiveRecord::Migration
def change
create_table :#{plural_name}, force: true do |t|
t.string :title
t.integer :color_id
t.integer :size_id
t.datetime :created_at
t.datetime :updated_at
t.integer :base_product_id
t.integer :stock_qty
t.timestamps
end
end
end
FILE
end
end
Make sure you include the timestamp when you are generating your own migrations
(not using rails g migration)
Your generator should include something this:
create_file "db/migrate/#{Time.now.strftime("%Y%m%d%H%M%S")}_create_#{plural_name}.rb", <<-FILE
the .strftime method is a way to get the timestamp into your migration file the same way rails has it formatted.
I am RoR noob so this might be a simple problem for someone. I have created two models - User and Feedback and I have two tables associated with them. users and feedbacks.
Now I want to create a relationship table with user_id as one column and feeback_id as the other column.
Do I create a model or just a migration? I am confused.
Here are my user and feedback migrations.
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "phone"
t.string "password_hashed"
t.string "password_salt"
t.boolean "isdeleted", :default => false
t.timestamps
end
end
def self.down
drop_table :users
end
end
class CreateFeedbacks < ActiveRecord::Migration
def self.up
create_table :feedbacks do |t|
t.text "feedback"
t.integer "rating"
t.boolean "isdeleted", :default => false
t.timestamps
end
end
def self.down
drop_table :feedbacks
end
end
Now do I create a model??? >rails generate model FeedbackUserJoinTable ? Or just a migration like this ??
class CreateFeedbackUserJoinTable < ActiveRecord::Migration
def change
create_table :feedbacks_users, :id => false do |t|
t.integer :feedback_id
t.integer :user_id
end
end
end
I like this answer to another SO question on has_and_belongs_to_many. Basically use HABTM relationships if the relationships between users and feedbacks will remain simple.
If you foresee that these relationships won't remain simple, you should create a join model between the two and use has_many ..., :through => .... That way, when you want to add properties to the relationships between a particular pair of User and Feedback objects, you can define those properties on the join model.
You do not need to create a model. Just create the migration as you have written :)