dear developers I have some problems with Rails models
Here is my sql tables
create_table "areas", :primary_key => "ndc", :force => true do |t|
t.string "townname", :limit => 256, :null => false
end
create_table "books", :primary_key => "ndc", :force => true do |t|
t.integer "booked", :null => false
t.integer "free", :null => false
end
class Book < ActiveRecord::Base
self.primary_key = "ndc"
has_one :area, :foreign_key => 'ndc'
end
class Area < ActiveRecord::Base
self.primary_key = "ndc"
belongs_to :book , :foreign_key => 'ndc'
end
in controller I have such code
#books = Book.paginate :page => params[:page] || 1, :per_page => 10
#books.each do |booking|
p booking.area
p booking
end
In production mode doesn't work, booking.area is nil object. what it can be ?
Area becames nil if config.cache_classes = true
so booking.area generates such queries
if cashe_classes = true
SELECT areas.* FROM areas WHERE (areas.ndc = NULL) LIMIT 1
but without cashing classes
SELECT areas.* FROM areas WHERE (areas.ndc = 30) LIMIT 1
FIXED by removing belongs_to :book , :foreign_key => 'ndc' from area class.
Your areas table needs a book_id integer field to match against the books table's primary key.
Related
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.
In Rail, I have this schema (for a join table between car and user habtm)
create_table "cars_users", :id => false, :force => true do |t|
t.integer "user_id"
t.integer "car_id"
end
add_index "cars_users", ["car_id", "user_id"], :name => "index_cars_users_on_car_id_and_user_id"
add_index "cars_users", ["user_id", "car_id"], :name => "index_cars_users_on_user_id_and_car_id"
but due to some bug, I have duplicate rows in this table.
shouldn't the index handle that?
Try:
add_index :cars_users, [ :user_id, :car_id ], :unique => true, :name => 'by_user_and_car'
it will raise an exception but you can add your own validation
class User < ActiveRecord::Base
has_and_belongs_to_many :cars, :before_add => :validates_car
def validates_car(car)
errors.add(:car, "already created" ) if self.cars.include? car
end
end
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
I have a model Country (which is the same as 'Team') and a model Match and I am trying to build a scenario where I have a Match record with both home & away teams.
The models
class Country < ActiveRecord::Base
has_many :home_matches, :foreign_key => 'home', :class_name => "Match"
has_many :away_matches, :foreign_key => 'away', :class_name => "Match"
end
class Match < ActiveRecord::Base
belongs_to :home, :class_name => "Country", :foreign_key => "home"
belongs_to :away, :class_name => "Country", :foreign_key => "away"
end
The schemas
create_table "countries", :force => true do |t|
t.string "name"
t.text "bio"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "matches", :force => true do |t|
t.datetime "matchdate"
t.integer "home"
t.integer "away"
t.datetime "created_at"
t.datetime "updated_at"
end
Problem
This works well if I just want:
> canada.away_matches
> japan.home_matches
But how do I get all matches that a country is playing?
Update:
I found the answer in another reply.
ActiveRecord has two association
I have updated my Country model with the following code:
def matches
Match.where("home = ? OR away = ?", self, self)
end
Now I can query:
> canada.home_matches
> canada.away_matches
> canada.matches
And get the desired result.
You are setting up the associations in a wrong way.
Go through this
Single_Table_Inheritance wiki, and
single-table-inheritance-and-where-to-use-it-in-rails
I am struggling with a polymorphic many-to-many association in rails 3.
My Article model looks like this:
class Article < ActiveRecord::Base
has_many :article_tags
has_many :people, :through => :article_tags, :source => :taggable, :source_type => :person
end
My ArticleTag model looks like this:
class ArticleTag < ActiveRecord::Base
belongs_to :article
belongs_to :taggable, :polymorphic => true
end
My Person model looks like this:
class Person < ActiveRecord::Base
has_many :article_tags, :as => :taggable
end
Finally, my Organization model looks like this:
class Organization < ActiveRecord::Base
has_many :article_tags, :as => :taggable
end
I have a schema which looks like this:
ActiveRecord::Schema.define(:version => 20110322234836) do
create_table "article_tags", :force => true do |t|
t.integer "taggable_id"
t.string "taggable_type"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "articles", :force => true do |t|
t.string "title"
t.text "content"
t.date "published_on"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "people", :force => true do |t|
t.string "first_name"
t.string "last_name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "organizations", :force => true do |t|
t.string "title"
t.datetime "created_at"
t.datetime "updated_at"
end
end
I was hoping that this would allow me to create people and organizations relationships from my articles like this:
Article.create!
Article.first.people.create!
and hopefully to be able to access it the other way afterwards by
Person.first.articles
Unfortunately I get an error when I try to add a person this way:
Article.first.people.create!
NameError: uninitialized constant Article::person
.../base.rb:1199:in `compute_type'
.../reflection.rb:162:in `klass'
.../association_collection.rb:157:in `transaction'
.../has_many_through_association.rb:41:in `create_record'
.../has_many_through_association.rb:13:in `create!
Any help would be very much appreciated I've tried many alternatives but with no success.
I actually just had to some something similar to this recently. It looks like most of your code is right although in mine for the first section I would try changing do this:
class Article < ActiveRecord::Base
has_many :article_tags
has_many :people, :through => :article_tags, :source => :taggable, :source_type => "Person"
end
Also you can't do: Article.first.people.create({:name => "Tim"})
You would have to assign it to a variable first:
article = Article.first
article.people.create({:name => "Tim"})
Let me know if this works for you. I just skimmed through. If it doesn't i can double check my code again and see if there are any other differences.