How to call references to other tables in a model rails - sql

I have a model called cause.rb which has a scope called pesquisa that I want to query in the table causes, fields that are references to other tables. I want to call the table places which have a field called city from the model cause.
Causes table:
class CreateCauses < ActiveRecord::Migration
def change
create_table :causes do |t|
t.string :title, null: false
t.text :description, null: false
t.boolean :anonymous, default: false
t.integer :count_likes, default: 0
t.integer :count_dislikes, default: 0
t.integer :count_visits, default: 0
t.references :user
t.references :category
t.timestamps
end
add_index :causes, :user_id
add_index :causes, :category_id
end
end
class AddPlaceIdToCauses < ActiveRecord::Migration
def change
add_column :causes, :place_id, :integer
end
end
Places table:
class CreatePlaces < ActiveRecord::Migration
def change
create_table :places do |t|
t.string :description
t.decimal :latitude
t.decimal :longitude
t.references :city
t.timestamps
end
add_index :places, :city_id
end
end
Here is the scope:
scope :pesquisa, ->(pesquisa) { where("cause.place.city like :p or category_id like :p ", p: "%#{pesquisa}%") }
default_scope :order => "id desc"
How do I resolve this problem? I try this above cause.place.city to call the city in the table places but nothing happens.

Related

Pull data from multiple tables in Rails

I am new in Ruby on Rails and I am trying to make a book registration website. Everything works fine except category section. When a user assign a category to his book, my database copies book_categories.id and put it in book.book_categories_id. The website also have a profile page, where you can view user's book(s). My problem is to display a category.name, I searched a lot of similar problems but I have not found the right answer.
Here is my book controller:
before_action :set_book, except: [:index, :new, :create]
before_action :authenticate_user!, except: [:show]
def show
#photos = #book.photos
end
def index
#books = current_user.books
end
def new
#book = current_user.books.build
end
def create
#book = current_user.books.build(books_params)
if #book.save
redirect_to listing_book_path(#book), notice: "Saved."
else
flash[:alert] = "Failed."
render :new
end
end
private
def set_book
#book = Book.find(params[:id])
end
def book_params
params.require(:book).permit(:book_categories_id, :book_name, :summary, :address, :price, :company_name)
end
As long as I understand, I have to allow my controller to have access to my category table but I do not know how. Also, all of my categories store in seed.rb.
This is my BookCategory.rb model:
class BookCategory < ActiveRecord::Base
has_many :books, :foreign_key => :book_categories_id
end
Book.rb model:
class Book < ApplicationRecord
belongs_to :user, :foreign_key => 'user_id'
has_many :photos, dependent: :delete_all
validates :book_name, presence: true
validates :book_categories_id, presence: true
def cover_photo
if self.photos.length > 0
self.photos[0].image.url
else
"default/image-default.jpg"
end
end
end
My schema.rb:
create_table "book_categories", force: :cascade do |t|
t.string "name"
t.string "subcategory"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "book", force: :cascade do |t|
t.string "book_name"
t.text "summary"
t.string "address"
t.decimal "price", precision: 8, scale: 2
t.boolean "active"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.float "latitude"
t.float "longitude"
t.string "company_name"
t.integer "book_categories_id"
t.index ["book_categories_id"], name: "index_books_on_book_categories_id"
end
Usually, when I type #book_categories.name, I get an error:
undefined method `name' for nil:NilClass
And at the very end, it says Parameters: {"id"=>"15"} whereas the category.id is different, and it passes book.id instead.
What I am doing wrong?
First fix the naming problem you have going. I don't know how the books table got called "book" instead of "books" in the schema, but you might have to fix that first.
Then category should be singular everywhere except the database table name. You need to make a migration to fix the foreign key and its index in the books table something like this:
class FixCategoryNaming < ActiveRecord::Migration
def change
remove_column :books, :book_categories_id
add_reference :books, :book_category, index: true, foreign_key: true
end
end
Then run rails db:migrate and check the schema looks ok
Then in the BookCategory class change the line to simply
has_many :books
You need to add this line to your Book class
belongs_to :book_category
and change the validation for book_category to be this
validates :book_category_id, presence: true
Then in the view for book index you have a #books variable. You can get the book name and category name for each book like this:
<% #books.each do |book| %>
<p><%= book.book_name %></p>
<p><%= book.book_categories.name %></p>
<% end %>
I think you need to setup as has_many through relationship. This is a good place to start http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

Ruby on Rails Has_Many :Through Association

I'm having some problems with ruby on rails, specifically setting up a many-to-many connection with deal and event through deal_event. I've checked out several similar stackoverflow questions and even http://guides.rubyonrails.org/ but I'm still not getting something..
Here are my models:
deal.rb
class Deal < ActiveRecord::Base
has_many :deal_events
has_many :events, :through => "deal_events"
attr_accessible :approved, :available, :cents_amount, :dollar_amount, :participants, :type
end
event.rb
class Event < ActiveRecord::Base
has_many :deal_events
has_many :deals, :through => "deal_events"
attr_accessible :day, :image, :description, :location, :title, :venue, :remove_image
end
deal_event.rb
class DealEvent < ActiveRecord::Base
belongs_to :deal
belongs_to :event
end
And here are my migration files:
20130102150011_create_events.rb
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
t.string :title, :null => false
t.string :venue
t.string :location
t.text :description
t.date :day
t.timestamps
end
end
end
20130112182824_create_deals.rb
class CreateDeals < ActiveRecord::Migration
def change
create_table :deals do |t|
t.integer :dollar_amount
t.integer :cents_amount
t.integer :participants
t.string :type, :default => "Deal"
t.integer :available
t.string :approved
t.timestamps
end
end
end
20130114222309_create_deal_events.rb
class CreateDealEvents < ActiveRecord::Migration
def change
create_table :deal_events do |t|
t.integer :deal_id, :null => false
t.integer :event_id, :null => false
t.timestamps
end
end
end
After I seed my database with one deal and one event, I go into the console and type in
deal = Deal.first # ok
event = Event.first # ok
DealEvent.create(:deal => deal, :event => event) # Error: ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: deal, event
deal.events # Error: ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association "deal_events" in model Deal
Any idea on what I'm doing wrong for these two errors to be popping up? Thanks.
You'll need this line in your DealEvent model:
attr_accessible :deal, :event
Although if it's just a relationship table (which it looks like) then you don't create the relationship that way. Use nested forms and the like.

has_many :through NameError: uninitialized constant

I just want to make a little join table, eventually storing extra info on that join (which is why I'm not using HABTM). From the rails documentation of associations I've created the following models:
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, :through => :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physicians
belongs_to :patients
end
my schema looks like this:
ActiveRecord::Schema.define(:version => 20130115211859) do
create_table "appointments", :force => true do |t|
t.datetime "date"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "patient_id"
t.integer "physician_id"
end
create_table "patients", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "physicians", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end
When I'm in the console and I create a physician and patient instance:
#patient = Patient.create!
#physician = Physician.create!
And try to associate one to the other
#physician.patients << #patient
I get
NameError: uninitialized constant Physician::Patients
Questions about this example have been asked before but none have address my scenario. Any ideas?
Thanks,
Neil, rails newbie.
The belongs_to calls in your Appointment model should take a singular form, not a plural form:
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end

Give composite primary key in Rails

How can i give composite primary key in Rails without any gem?
My first table in migration file:
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :userid
t.string :name
t.string :address
t.timestamps
end
end
def self.down
drop_table :users
end
end
My second table in migration file:
class CreateProjects < ActiveRecord::Migration
def self.up
create_table :projects do |t|
t.string :title
t.string :description
t.timestamps
end
end
def self.down
drop_table :projects
end
end
In my schema file:
ActiveRecord::Schema.define(:version => 20110222044146) do
create_table "projects", :force => true do |t|
t.string "title"
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", :force => true do |t|
t.string "userid"
t.string "name"
t.string "address"
t.datetime "created_at"
t.datetime "updated_at"
end
end
Now I want to create a table called User_has_project in which I will refer to User and Project that means will have 2 foreign keys.
So I tried like this:
class CreateUser_has_projects < ActiveRecord::Migration
def self.up
create_table :user_has_projects do |t|
t.references :User
t.references :Project
t.boolean :status
t.timestamps
end
end
def self.down
drop_table :users
end
end
Now how can I set combination of user_id and project_id as a primary key in user_has_projects?
It looks like you're trying to specify a many-many relationship between Users and Projects, with an additional field on the relationship itself.
The way you're currently doing isn't the Rails way of doing things - especially with the concept of a composite primary key.
The Rails/ActiveRecord way of doing this sort of relationship modelling is to have a third model that describes the relationship between User and Project. For the sake of example, I'm going to call it an Assignment. All you need to do is re-name your user_has_projects table to assignments like so:
class CreateAssignments < ActiveRecord::Migration
def self.up
create_table :assignments do |t|
t.references :user
t.references :project
t.boolean :status
t.timestamps
end
end
def self.down
drop_table :assignments
end
end
And then, in your model files:
# app/models/user.rb
class User < ActiveRecord::Base
has_many :assignments
has_many :projects, :through => :assignments
end
# app/models/assignment.rb
class Assignment < ActiveRecord::Base
belongs_to :user
belongs_to :project
end
# app/models/project.rb
class Project < ActiveRecord::Base
has_many :assignments
has_many :users, :through => :assignments
end
You can read more about this here: http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association

Multilevel Complex form with formtastic?

I have a scenario which needs a very complex form, and i need help in it.
I have three tables
create_table "permissions", :force => true do |t|
t.boolean "can_read"
t.boolean "can_create"
t.boolean "can_edit"
t.boolean "can_delete"
t.integer "role_id"
t.integer "resource_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "resources", :force => true do |t|
t.string "class_name"
t.string "class_action"
t.text "description"
t.integer "parent_resource"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "roles", :force => true do |t|
t.string "name"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
with the models and associations
class Role < ActiveRecord::Base
has_many :user_roles
has_many :users, :through => :user_roles
has_many :permissions
def to_s
self.name
end
end
class Resource < ActiveRecord::Base
has_many :permissions
has_many :children, :class_name => "Resource", :foreign_key => "parent_resource"
scope :root, lambda {
{
:conditions => "parent_resource IS NULL"
}
}
end
class Permission < ActiveRecord::Base
belongs_to :role
belongs_to :resource
end
Suppose we have 2 roles, admin, user, this time, i need a form structure like the image in the this link
How can i make this form? Thanks in advance.
I created a gem that makes it easier to handle nested forms inside formtastic: formtastic_cocoon.
That should get you started.