Rails associations has_many through ban/archive solution? - ruby-on-rails-3

I'm new in Rails and am working on a problem. I have two tables:
Shoes and Socks
A Shoe can have many Socks, but only one active Sock. Other Socks are inactive. All Socks are also unique with unique patterns. I want to make sure I don't have socks with the same pattern. I think I can do this three ways
1) Using an additional column in table socks to represent the active sock
class Shoe < ActiveRecord::Base
has_many :socks
end
class Sock < ActiveRecord::Base
belongs_to :shoe
end
class CreateGettingDressed < ActiveRecord::Migration
def change
create_table :shoes do |t|
t.string :size
t.timestamps null: false
end
create_table :socks do |t|
t.belongs_to :shoe, index:true
t.string :pattern
t.boolean :active
t.timestamps null: false
end
end
end
This seems fairly simple, but cumbersome. I would search socks with shoe_id, and pull out the active_sock and return it's pattern. I think I would index [active_sock, shoe_id] in an array to speed it up.
2) Using an additional table to archive inactive socks
class Shoe < ActiveRecord::Base
has_many :socks
has_many :inactive_socks
end
class Sock < ActiveRecord::Base
belongs_to :Shoe
end
class Inactive_sock < ActiveRecord::Base
belongs_to :Shoe
end
class CreateGettingDressed < ActiveRecord::Migration
def change
create_table :shoes do |t|
t.string :name
t.timestamps null: false
end
create_table :socks do |t|
t.belongs_to :shoe, index:true
t.string :pattern
t.timestamps null: false
end
create_table :inactive_socks do |t|
t.belongs_to :shoe, index:true
t.string :pattern
t.timestamps null: false
end
end
end
This seems cumbersome as well, but when you are just dealing with active socks easy to use and fast. But when buying a new sock, I have to check the pattern with both tables.
3) Using a has_many :through relationship
class Shoe < ActiveRecord::Base
has_many :active_socks
has_many :socks, through: active_socks
end
class Active_Sock < ActiveRecord::Base
belongs_to :Shoe
belongs_to :Sock
end
class Sock < ActiveRecord::Base
has_many :active_socks
has_many :shoes, through: active_socks
end
class CreateGettingDressed < ActiveRecord::Migration
def change
create_table :shoes do |t|
t.string :name
t.timestamps null: false
end
create_table :socks do |t|
t.string :pattern
t.timestamps null: false
end
create_table :active_socks do |t|
t.belongs_to :shoe, index: true
t.belongs_to :sock, index: true
t.string :pattern
t.boolean :active
t.timestamps null: false
end
end
end
This seems like option 2, but I feel like I'm using Rails tools to make it less cumbersome. When I'm searching for patterns I'm just checking the socks table, when I'm searching for the one active_sock I'm just searching active_socks table.
I've read up on similar posts, and it seems options 1 and 2 are commonly used in closed_accounts, banning users, banning posts, archiving etc. Situations where you need to differentiate data that is only slightly different. The choice there seems to be look at what you need and choose the option 1 or 2 that best fits you.
My understanding for has_many through situations seems to be when you have a relationship and you need extra meta data you can use it. I think that fits this situation.
Did I set up option 1 correctly and am I right that indexing the array of [shoe_id and active] will give me a faster search? Is option 3 an appropriate use of has_many through? Would my explanation of option 3 work?

What are your usage patterns? I'm guessing you just want to be able to find the active Sock given a Shoe, and if a given Sock is active or inactive. To quickly find the active Sock of a given Shoe, you merely need to give the Sock a foreign key to its Shoe with the belongs_to association.
class Sock < ActiveRecord::Base
belongs_to :shoe
end
And to find out if a Sock is active or inactive, give its owner shoe a reference to its active sock like so:
class Shoe < ActiveRecord::Base
belongs_to :sock
end
Now, you can just go to its owner Shoe and check if the Shoe's active sock is the current Sock or not. E.g.
def is_active
owner_shoe.active_sock == self
Basically, associate them with foreign keys and you're good to go!
P.S. you pluralized Socks but the Rails convention is to use singular for model names.
EDIT: You asked for a migration so here's one for the code above. Caveat: I haven't done migrations in a long time in Rails so something might be off.
class CreateGettingDressed < ActiveRecord::Migration
def change
create_table :shoes do |t|
t.belongs_to :active_sock, foreign_key: "sock_id"
t.string :size
t.timestamps null: false
end
create_table :socks do |t|
t.belongs :owner_shoe, foreign_key: "shoe_id"
t.string :pattern
end
end
end

Related

Can I have two foreign key from one table to an other, in order to get two columns field?

I have two tables one Player and the other Result. Can I set two foreign key on Result, related to Player, so that I can change the value of an two players with the Result? I am working on Ruby web application where I will update the fields for each Player according to the results I am going to add that are related to a football game. For example, if the score is Player1 3 - 0 Player2, Player1 will get 3 points while the other 0, so basically from one result I will have to change two fields from the table Player. I will also have to add the GoalsScored and GoalsTaken by each player. Can I sort this thing out assigning for example two names in result, I will check the name in the Player table and then change the fields, using two foreign keys, or I need just to set my methods correctly?
Here are the tables:
Player
t.string :name
t.integer :win
t.integer :draw
t.integer :lose
t.integer :gs
t.integer :gt
t.integer :dr
t.integer :points
Result
t.string :name1
t.integer :goal1
t.string :name2
t.integer :goal2
t.datetime :date
t.references :player, null: false, foreign_key: true
Is it ok if I add two references to Result?
Yes, you can. And sometimes that's a good idea, but not here. You want a join table instead. Here's why.
You need to use two different column names and tell Rails what table they reference.
Result
t.string :name1
t.integer :goal1
t.string :name2
t.integer :goal2
t.datetime :date
t.references :player1,
null: false,
foreign_key: { to_table: :players }
t.references :player2,
null: false,
foreign_key: { to_table: :players }
And you'd need to make them explicit in your code.
class Result < ApplicationRecord
belongs_to :player1, class_name: 'Player'
belongs_to :player2, class_name: 'Player'
end
Tying them together in Player is a little tricker. The naive thing to do is this.
class Player < ApplicationRecord
has_many :player1_results,
class_name: 'Result',
foreign_key: :player1_id
has_many :player2_results,
class_name: 'Result',
foreign_key: :player2_id
end
What if you want all the Player's results? What if you want all the players of a Result? There's the problem. You need to make redundant queries or add extra clauses like where player1_id = :player_id or player2_id = :player_id. Similarly name1, name2, goal1, goal2.
Whenever you want to store more than one associated thing, you need a join table. Even if it's only two. It makes life much easier.
You have a result table, but a result of what? A match! Where's information about the match stored? In the result table. It should be its own table.
We have three tables. Players, Matches, and a table to store how a player did in a match (the result).
create_table :players do
t.string :name, null: fase
...
t.timestamps
end
create_table :matches do
t.string :name, null: false
t.datetime :date, null: false
...
t.timestamps
end
create_table :results do
t.references :player, foreign_key: true, null: false
t.references :match, foreign_key: true, null: false
t.integer :goals
end
Now with all three pieces, we can put them together. The Matches and Players are related through Results.
class Matches < ApplicationRecord
has_many :results
has_many :players, through: :results
end
class Players < ApplicationRecord
has_many :results
has_many :matches, through: :results
end
class Results < ApplicationRecord
belongs_to :match
belongs_to :player
end
Now if you want to find the players in your match...
players = match.players
This will perform the join on results for you.
If you must track who is player 1 and player 2, add that to the Result table with a unique constraint.
create_table :results do
t.references :player, foreign_key: true, null: false
t.references :match, foreign_key: true, null: false
t.integer :goals, null: false, default: 0
t.integer :player_number, null: false, default 1
# Can't have two player 1s for the same match.
# :player_number is deliberately first so this index also serves
# to index player_number.
t.index [:player_number, :match_id], unique: true
end
Then you can get player 1 like so:
player = match.players.find_by!(player_number: 1)
And you can add some convenience methods to the relationship.
class Matches < ApplicationRecord
has_many :results
has_many :players, through: :results do
def player(num)
match.players.find_by!(player_number: num)
end
end
end
player = match.players.player(1)

Table belonging to two models?

I want to have a model for a Baseball team that has many players and statistics about the team.
I also want a model for players that have the same statistics.
How can I have the Statics table belong to both the Baseball Model and the Player Model?
You can use Polymorphic association. You can refer here
Statistic model
class Statistic < ActiveRecord::Base
belongs_to :statisticable, polymorphic: true
end
Baseball model
class Baseball < ActiveRecord::Base
has_many :statistic, as: :statisticable
end
Player model
class Player < ActiveRecord::Base
has_many :statistic, as: :statisticable
end
Your migration file for Statistic model
class CreateStatistic < ActiveRecord::Migration
def change
create_table :statistics do |t|
t.string :strength
t.string :defensive
t.string :attack
t.references :statisticable, polymorphic: true, index: true
t.timestamps null: false
end
end
end

Change primary and foreign key in ActiveRecord

I have two models :
class Settlement < ActiveRecord::Base
set_primary_key :settlement_identifier
has_many :streets
attr_accessible :city, :name, :service_available, :zip, :country_id,: settlement_identifier
end
class Street < ActiveRecord::Base
belongs_to :settlement, foreign_key: "settlement_identifier"
attr_accessible :name, :settlement_identifier, :street_identifier
end
Because I am doing import for streets and settlements, I need to point streets via settlement_identifier, not settlement_id .
When I do
Street.first.settlement #it compare settlement_identifiers from both tables
But when try to get streets from single settlement like :
Settlement.first.streets
It throws an error
SELECT "streets".* FROM "streets" WHERE "streets"."settlement_id" = 4263
ActiveRecord::StatementInvalid: PG::Error: ERROR: column streets.settlement_id does not exist .
I want that query to be :
SELECT "streets".* FROM "streets" WHERE "streets"."settlement_identifier" = 4263
Any help ?
I solved this problem. Here is solution below :
class CreateSettlements < ActiveRecord::Migration
def change
create_table :settlements, primary_key: :settlement_identifier, id: :false do |t|
t.string :name
t.string :zip
t.string :city
t.string :service_available
t.integer :country_id
t.timestamps
end
end
def down
drop_table :settlements
end
end
Here I set primary_key in my migration to settlement_identifier, and set id to false
Also, my Street migration is:
class CreateStreets < ActiveRecord::Migration
def change
create_table :streets do |t|
t.string :name
t.integer :settlement_identifier
t.string :street_identifier
t.timestamps
end
end
end
So, Street has reference to Settlement via settlement_identifier .
Settlement model :
class Settlement < ActiveRecord::Base
has_many :streets, foreign_key: "settlement_identifier"
attr_accessible :city, :name, :service_available,:settlement_identifier
end
Street model :
class Street < ActiveRecord::Base
belongs_to :settlement, foreign_key: "settlement_identifier"
attr_accessible :name, :settlement_identifier, :street_identifier
end
I tried to set primary_key on Settlement model but that didn't work.
This works fine for me. If anyone have another solution, please put comment or code example.

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.

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