Setting up the database, I am just curious if I did it correctly, as it looks a bit off. There are people, who have a user account, and a role (teacher or student). they are participants in a class (where a class has many students and teachers; a student has many classes; a teacher has many classes). I think my class_instruction model is off in the DB, but please tell me if it will work, or if there is a better way (like maybe with a has_many_through table of participants)
schema.rb:
ActiveRecord::Schema.define(:version => 20130524160107) do
create_table "class_instructions", :force => true do |t|
t.string "name"
t.datetime "time"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "person_id"
end
add_index "class_instructions", ["person_id"], :name => "index_class_instructions_on_person_id"
create_table "people", :force => true do |t|
t.string "firstName"
t.string "lastName"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "people", ["user_id"], :name => "index_people_on_user_id"
create_table "roles", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "user_roles", :force => true do |t|
t.integer "user_id"
t.integer "role_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "user_roles", ["role_id"], :name => "index_user_roles_on_role_id"
add_index "user_roles", ["user_id"], :name => "index_user_roles_on_user_id"
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
My concern is that the person_id is part of the class. Is this correct?
ClassInsturction.rb :
class ClassInstruction < ActiveRecord::Base
belongs_to :people
has_many :cassignments
has_many :assignments, :through => :cassignments
def className
self.name
end
def classAssignments
return self.cassignments
end
end
I would make a joining table that has both class and people and use has many through. If people have many classes and classes have many people you can not do this any other way.
I have seen usually belongs_to with singular form:
belongs_to :person
I am not sure if this is what you are asking for though.
Related
I get this error:
rake aborted!
undefined method `meta_keywords' for Page::Translation:0x0000000548d5f0
when running rake db:setup on a refinery cms 1.0.0 rails 3.0.7.
and can't quite debug it
Edit
part of schema:
create_table "page_part_translations", :force => true do |t|
t.integer "page_part_id"
t.string "locale"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "page_part_translations", ["page_part_id"], :name => "index_page_part_translations_on_page_part_id"
create_table "page_parts", :force => true do |t|
t.integer "page_id"
t.string "title"
t.text "body"
t.integer "position"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "page_parts", ["id"], :name => "index_page_parts_on_id"
add_index "page_parts", ["page_id"], :name => "index_page_parts_on_page_id"
create_table "page_translations", :force => true do |t|
t.integer "page_id"
t.string "locale"
t.string "title"
t.string "custom_title"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "page_translations", ["page_id"], :name => "index_page_translations_on_page_id"
create_table "pages", :force => true do |t|
t.integer "parent_id"
t.integer "position"
t.string "path"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "show_in_menu", :default => true
t.string "link_url"
t.string "menu_match"
t.boolean "deletable", :default => true
t.string "custom_title_type", :default => "none"
t.boolean "draft", :default => false
t.boolean "skip_to_first_child", :default => false
t.integer "lft"
t.integer "rgt"
t.integer "depth"
end
add_index "pages", ["depth"], :name => "index_pages_on_depth"
add_index "pages", ["id"], :name => "index_pages_on_id"
add_index "pages", ["lft"], :name => "index_pages_on_lft"
add_index "pages", ["parent_id"], :name => "index_pages_on_parent_id"
add_index "pages", ["rgt"], :name => "index_pages_on_rgt"
pages.rb: https://gist.github.com/grzegorzhauska/dcca44f3e2091c21400a
pages_for_inqueries.rb: https://gist.github.com/grzegorzhauska/997c355c2ce09d1da624
The reason you get this error is because meta_keywords used to exist, but now it doesn't. Make sure you are using an up to date version of the seo_meta library.
I have two models...
create_table "registrations", :force => true do |t|
t.integer "orientation_id"
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "student_id"
...
end
create_table "orientations", :force => true do |t|
t.date "class_date"
t.text "class_time"
t.integer "seats"
t.boolean "active", :default => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
I want to create a validation in my registration model that says the student_id must be unique in each Orientation.
If i understood your question correctly,you want the scope option of the validates_uniqueness_of.If so,this should work
In your Registration model,
Class Registration < ActiveRecord::Base
......
validates_uniqueness_of :student_id, scope: :orientation_id
end
And also,you should be generating a migration to add this
add_index :registration, [ :student_id, :orientation_id ], :unique => true
More Info here
Why Why ActiveRecord::StatementInvalid: SQLite3::SQLException: near ")": syntax error: INSERT INTO "user_friendships" () VALUES ()
When trying to test:
$ ruby -I test test/unit/user_friendships_test.rb
require 'test_helper'
class UserFriendshipsTest < ActiveSupport::TestCase
should belong_to (:user)
should belong_to (:friend)
test "that creating a frinedship works without raising an exception" do
assert_nothing_raised do
UserFriendship.create user: users(:a), friend: friends(:b)
end
end
end
Any idea?
UPDATE: his is part of the Schema.rb
create_table "user_friendships", :force => true do |t|
t.integer "user_id"
t.integer "friend_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "user_friendships", ["user_id", "friend_id"], :name => "index_user_friendships_on_user_id_and_friend_id"
create_table "users", :force => true do |t|
t.string "first_name"
t.string "last_name"
t.string "profile_name"
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0, :null => false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
I ran into this as well. It seems to be related to screwing up the rails naming conventions. In my case, doing "rails g model activity_item" instead of "rails g model activity_items" solved the issue.
I am developing a rails app where users can post, must like facebook. I want to implement a notification systems that alerts users to new posts. However, I am having trouble on how to tell if a user has viewed posts or not. I am literally clueless.
I am using devise gem which gives me access to certain user stats (if this helps):
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.integer "failed_attempts", :default => 0
t.string "unlock_token"
t.datetime "locked_at"
t.string "authentication_token"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "username", :default => "", :null => false
t.integer "admin", :default => 0
end
And my post model:
create_table "posts", :force => true do |t|
t.integer "user_id"
t.text "content"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
How can I implement a system that knows if a user has seen a post or not?
simple aproach would be like that:
create a model called Seen
rails g model Seen post:references user:references
models/seen.rb
belongs_to :user
belongs_to :post
models/user.rb
has_many :seens
has_many :seen_posts, through: :seens, source: :post
models/post.rb
has_many :seens
has_many :seen_users, through: :seens, source: :user
and you can create a method something like that
models/post.rb
def seen_by?(user)
seen_user_ids.include?(user.id)
end
controllers/posts_controller.rb
def show
#post = Post.find(params[:id])
current_user.seen_posts << #post unless #post.seen_by?(current_user)
end
I have the following models:
class Constraint < ActiveRecord::Base
belongs_to :constraint_category
end
class ConstraintCategory < ActiveRecord::Base
has_many :constraints
end
The models have these attributes (from db/schema.rb):
create_table "constraint_categories", :force => true do |t|
t.string "value"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "active"
end
create_table "constraints", :force => true do |t|
t.string "phrase"
t.integer "constraint_category_id", :limit => 255
t.boolean "active"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
I would like to create a query that finds all constraints where the "active" attribute is "true" and the "constraint_category.value" is "Noun".
Would love any advice on getting there.
Constraint.joins(:constraint_category).where('constraints.active = ? and constraint_categories.value = ?', true, 'Noun')
See conditions and joins in the guide.