AssociationTypeMismatch problem when saving data - ruby-on-rails

in a previous question i was stumped.. but stack overflow provided the solution
my two models
class Team < ActiveRecord::Base
has_many :fixtures, :finder_sql => 'SELECT * FROM fixtures where (home_team = #{id} or away_team = #{id})'
has_many :home_fixtures, :class_name => "Fixtures", :foreign_key => :home_team
has_many :away_fixtures, :class_name => "Fixtures", :foreign_key => :away_team
has_many :teamalias
end
class Fixture < ActiveRecord::Base
belongs_to :league
belongs_to :selection
has_many :selection
named_scope :for_team_id, lambda{|team_id| {:conditions => ['(home_team = ? or away_team = ?)', team_id, team_id]} }
belongs_to :home_team, :class_name => "Team", :foreign_key => :home_team
belongs_to :away_team, :class_name => "Team", :foreign_key => :away_team
def fix_list
[home_team.title, "Draw", away_team.title]
end
end
taken from
Multi level associations in rails
but I'm stumped again- i'm trying to save a fixture based on the last solution in the first answer above and I am getting a typemismatch of.
Team(#38391330) expected, got String(#1242130)
no idea what to do here please help.
edit- db migrations
Here is the migrations
class CreateFixtures < ActiveRecord::Migration
def self.up
create_table :fixtures do |t|
t.integer :home_team
t.integer :away_team
t.datetime :when
t.integer :league_id
t.timestamps
end
end
def self.down
drop_table :fixtures
end
end
class CreateTeams < ActiveRecord::Migration
def self.up
create_table :teams do |t|
t.string :title
t.timestamps
end
end
def self.down
drop_table :teams
end
end
class AddResultToFixture < ActiveRecord::Migration
def self.up
add_column :fixtures, :result, :integer
end
def self.down
remove_column :fixtures, :result
end
end

your form probably has fixture[home_team] which is a select which passes the team.id
so when you do
#fixture = Fixture.new(params[:fixture])
#fixture.save
you are calling home_team= team.id team.id is a string but home_team should be a Team object

Related

migration with two belongs_to for the same table

I have the next two models
class Currency < ActiveRecord::Base
# something ...
end
class CurrencyRate < ActiveRecord::Base
attr_accessible :rate
belongs_to :currency_to_convert, :class_name => 'Currency'
belongs_to :currency_converted, :class_name => 'Currency'
end
how should I create the migration for the CurrencyRate model, only with one field for the foreign key association or two fields?
I have been thinking in something like
class CreateCurrencyRates < ActiveRecord::Migration
def change
create_table :currency_rates do |t|
t.integer :currency_id
t.timestamps
end
end
end
but I'm not sure if works for a model with two belongs_to associations.
class CurrencyRate < ActiveRecord::Base
attr_accessible :rate
belongs_to :currency_to_convert, :class_name => 'Currency', :foreign_key => 'currency_convert_id'
belongs_to :currency_converted, :class_name => 'Currency', :foreign_key => 'currency_converted_id'
end
class CreateCurrencyRates < ActiveRecord::Migration
def change
create_table :currency_rates do |t|
t.integer :currency_converted_id
t.integer :currency_convert_id
t.timestamps
end
end
end

rails undefined method 'has_many'

I'm making a database:
class CreateUsers < ActiveRecord::Migration
def change
has_many :listings, :dependent => :restrict #won't delete if listings exist
has_many :transactions, :dependent => :restrict #won't del if trans exist
create_table :users do |t|
t.integer :key #it's hard to use string as primary
t.string :identifier_url
t.string :username
t.integer :rating
t.timestamps
end
end
end
and
class CreateListings < ActiveRecord::Migration
def change
has_one :book
belongs_to :transaction
belongs_to :user
create_table :listings do |t|
t.integer :key
t.integer :condition
t.decimal :price
t.timestamps
end
end
end
I can't find anything anywhere on this so I'm guessing it's something really basic.
The associations (has_many, belongs_to etc...) should be declared in the model, not in the migration.
This is a good read to start with migrations:
http://guides.rubyonrails.org/migrations.html
And this one for associations:
http://guides.rubyonrails.org/association_basics.html
Put your association in the model
class Member < ActiveRecord::Base
has_many :listings, :dependent => :restrict
has_many :transactions, :dependent => :restrict
end
You don't have to declare the associations in the migration, but in the models!

Rename foreign_key to override in Rails Convention

I have a problem in association between two classes, so i have a class table here named Post
Class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :post_type , null: false
t.text :content , null: false
t.integer :person_id
end
add_index :posts, :person_id
add_index :posts, :group_id
end
end
and the other one is called Action
class CreateActions < ActiveRecord::Migration
def change
create_table :actions do |t|
t.string :target_type, null:false
t.integer :target_id
t.integer :upvote_count
t.timestamps
end
add_index :actions,:target_id
end
end
so the problem is i want to associate the target_is as the foreign key to the Post class so i did this
class Post < ActiveRecord::Base
has_one :action
end
class Action < ActiveRecord::Base
belongs_to :post , :class_name => 'Target', :foreign_key => 'target_id'
end
but is doesn't work, which when i assign Action object to action method in Post object this error is appeared
Mysql2::Error: Unknown column 'actions.post_id' in 'where clause': SELECT `actions`.* FROM `actions` WHERE `actions`.`post_id` = 6 LIMIT 1
so any help?
You need to set the foreign key on both sides of the association:
class Post < ActiveRecord::Base
has_one :action, :foreign_key => 'target_id'
end
class Action < ActiveRecord::Base
belongs_to :post , :class_name => 'Target', :foreign_key => 'target_id'
end
http://guides.rubyonrails.org/association_basics.html#has_one-association-reference
http://guides.rubyonrails.org/association_basics.html#belongs_to-association-reference
I suppose you are trying to apply polymorphic association. Try this out.
class Post < ActiveRecord::Base
has_one :action, :as => :target
end
class Action < ActiveRecord::Base
belongs_to :target, :polymorphic => true
end

belongs_to with :class_name option fails

I have no idea what went wrong but I can't get belongs_to work with :class_name option. Could somebody enlighten me. Thanks a lot!
Here is a snip from my code.
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.text :name
end
end
def self.down
drop_table :users
end
end
#####################################################
class CreateBooks < ActiveRecord::Migration
def self.up
create_table :books do |t|
t.text :title
t.integer :author_id, :null => false
end
end
def self.down
drop_table :books
end
end
#####################################################
class User < ActiveRecord::Base
has_many: books
end
#####################################################
class Book < ActiveRecord::Base
belongs_to :author, :class_name => 'User', :validate => true
end
#####################################################
class BooksController < ApplicationController
def create
user = User.new({:name => 'John Woo'})
user.save
#failed_book = Book.new({:title => 'Failed!', :author => #user})
#failed_book.save # missing author_id
#success_book = Book.new({:title => 'Nice day', :author_id => #user.id})
#success_book.save # no error!
end
end
environment:
ruby 1.9.1-p387
Rails 2.3.5
class User < ActiveRecord::Base
has_many :books, :foreign_key => 'author_id'
end
class Book < ActiveRecord::Base
belongs_to :author, :class_name => 'User', :foreign_key => 'author_id', :validate => true
end
The best thing to do is to change your migration and change author_id to user_id. Then you can remove the :foreign_key option.
It should be
belongs_to :user, :foreign_key => 'author_id'
if your foreign key is author id. Since you actually have User class, your Book must belong_to :user.
I do in this way:
Migration -
class AddAuthorToPosts < ActiveRecord::Migration
def change
add_reference :posts, :author, index: true
add_foreign_key :posts, :users, column: :author_id
end
end
class Post
belongs_to :author, class_name: "User"
migration
t.belongs_to :author, foreign_key: { to_table: :users }
working on rails 5

Problem with has_many :through Association in Ruby on Rails

I've got a problem with a has_many :through association, i cant call the u1.UsersProfileAttributes.find_by_ProfileAttribute_name("icq") method, rails means that this method doesn't exist. The method u1.UsersProfileAttributes.find_by_ProfileAttribute_id(3) works correctly. u1 is a user object. I don't know whats the problem, because my associations seems to be okay. Have a look:
class ProfileAttribute < ActiveRecord::Base
has_many :UsersProfileAttributes
has_many :users, :through => :UsersProfileAttributes
end
class User < ActiveRecord::Base
has_many :UsersProfileAttributes
has_many :ProfileAttributes, :through => :UsersProfileAttributes
end
class UsersProfileAttribute < ActiveRecord::Base
belongs_to :user
belongs_to :ProfileAttribute
end
My Migration file:
class CreateProfileAttributes < ActiveRecord::Migration
def self.up
create_table :profile_attributes do |t|
t.string :name
t.integer :profile_group_id
t.timestamps
end
create_table :users_profile_attributes do |t|
t.integer :user_id
t.integer :ProfileAttribute_id
t.string :value
end
end
def self.down
drop_table :profile_attributes
drop_table :users_profile_attributes
end
end
You need to query ProfileAttributes, not UsersProfileAttributes. This should work for you: u1.ProfileAttributes.find_by_name('icq')
u1.UsersProfileAttributes.find_by_ProfileAttribute_id(3) works because ProfileAttribute_id is an attribute of UsersProfileAttribute ... so ActiveRecord builds you a dynamic finder.
u1.UsersProfileAttributes.find_by_ProfileAttribute_name("icq") does not work because ProfileAttribute_name is not an attribute of UsersProfileAttribute.

Resources