I wish to set up nested comments, and want to use self-join to set this up.
class Comment < ActiveRecord::Base
has_many :children, :class_name => 'Comment'
#...
end
Now, what sql table structure would I use to set up the has_many self-join?
I'm assuming something like this:
comment_to_comments:
parent_id integer
child_id integer
How do I tell rails to use this table? How do I tell rails that parent_id is the foreign key to reach the parent and the child_id is the foreign key to reach the child?
create_table :comments do |t|
t.integer :parent_id
end
class Comment < ActiveRecord::Base
has_many :children, :class_name => "Comment", :foreign_key => :parent_id
belongs_to :parent, :class_name => "Comment"
end
I suggest you use plugin to implement this feature.like awesome_nested_set or acts_as_tree.
Related
I have a Model called Challenge that is created by a User. It has a Difficulty associated with it as well. When I create the Challenge and hand it the Author (User) and Difficulty, the Difficulty associate works, but the Author (User) one doesn't. The weirdest part is, when you look at the Challenge object, it shows the Author key its associated with.
challenge = Challenge.first
challenge.author (prints Nil) #Doesn't even run a Query
When I create a Challenge using the following code, the user_id is Nil.
user = User.find(1)
diff = Difficulty.find(1)
Challenge.create(:author => user, :difficulty => diff, :title => "My Challenge")
When I create a Challenge using this code, the User gets the relation to the Challenge and the Challenge gets the user_id of the User. But you can only go from User to Challenge. Challenge to User returns Nil.
user = User.find(1)
diff = Difficulty.find(1)
chall = Challenge.create(:difficulty => diff, :title => "My Challenge")
user.authored_challenges << chall
Here are my Models and Tables
# Models
class User < ActiveRecord::Base
has_many :authored_challenges, :class_name => "Challenge"
attr_accessible :display_name, :authored_challenges
end
class Reward < ActiveRecord::Base
has_many :challenges
attr_accessible :name, :value, :challenges
end
class Challenge < ActiveRecord::Base
belongs_to :reward
belongs_to :author, :class_name => "User"
attr_accessible :title, :description, :reward, :author
end
# Tables
create_table :users do |t|
t.string :display_name
t.timestamps
end
create_table :rewards do |t|
t.string :name
t.float :value
t.timestamps
end
create_table :challenges do |t|
t.integer :user_id
t.integer :reward_id
t.string :title
t.string :description
t.timestamps
end
According to: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html the foreign key should be:
:foreign_key => :author_id
Don't forget to add the field. Good Luck!
Have you tried:
belongs_to :author, :class_name => "User", :foreign_key => :user_id
From the Rails documentation:
By convention, Rails assumes that the column used to hold the foreign key on this model is the name of the association with the suffix _id added. The :foreign_key option lets you set the name of the foreign key directly
The example given in the docs is very similar to the one you have:
class Order < ActiveRecord::Base
belongs_to :customer, :class_name => "Patron", :foreign_key => "patron_id"
end
I have some troubles wrapping my head around the following situation..
I am trying to create a tree structure, where I will be able to give custom names to connections between nodes..
So I want to have Node and Relation models. Each
Node
has_many :relations
Each
Relation
has_many :nodes
Node can be either a parent or a child.. So far everything was easy and there are tons of examples that show how to make a self-referential has_many table... The problem is that I want to be able to give names to relations, so that I can do something like:
relation1 = node1.relations.create(:name => "relation_name", :child => node2)
and in result get something like:
relation1.name == "relation_name"
relation1.parent == node1
relation1.child == node2
All the creations are happening within the model, this activity is not really exposed to user, if that matters.
Thanks!
EDIT2:
Here is how it works now:
class Node < ActiveRecord::Base
belongs_to :sentence
has_one :parent_relation, :foreign_key => "child_id", :class_name => "Relation"
has_many :child_relations, :foreign_key => "parent_id", :class_name => "Relation"
has_one :parent, :through => :parent_relation
has_many :children, :through => :child_relations, :source => :child
has_many :relations, :foreign_key => "child_id"
has_many :relations, :foreign_key => "parent_id"
class Relation < ActiveRecord::Base
has_many :videos, :as => :videoable, :dependent => :destroy
has_many :phrases, :through => :videos
belongs_to :parent, :class_name => "Node"#, :inverse_of => :parent_relation
belongs_to :child, :class_name => "Node"#, :inverse_of => :child_relation
So what you're talking about is more like a Joins Model than a Self-Reference.
Note: I changed your relation association 'labels' because I was having a hard time with your naming, so you don't have to change your 'labels' that was just for me.
So for your Node class you could do something like this
class Node < ActiveRecord::Base
has_one :parent_relation, :foreign_key => "child_id",
:class_name => "Relation"
has_many :child_relations, :foreign_key => "parent_id",
:class_name => "Relation"
has_one :parent, :through => :parent_relation
has_many :children, :through => :child_relations, :source => :child
end
Then for your Relation class you could something like
class Relation < ActiveRecord::Base
belongs_to :parent, :class_name => "Node", :inverse_of => :parent_relation
belongs_to :child, :class_name => "Node", :inverse_of => :child_relations
end
The :inverse_of option should let you build let you build a Node based off the parent and children associations from your Node instances, this is just a caveat from the magic with :through relationships. (Documentation for this is at the bottom of the Joins Model section.)
I don't fully understand your association structure, but I think this should model it correctly. Lemme know if there are any problems though.
Side Note: Since Relation is a constant set in the ActiveRecord module you might consider changing it to something like NodeRelationship. I don't think it will interfere with your program, but it definitively caused some trouble for my thought process.
Hi people, long time not been here. But I'm back because I need your help again please. I have a rails 3.0.9 app, and I'm working with nested forms and has_many through association. When i create an instance, it works great. The problems come when I try to edit. Here is an example for a better explanation. (table names and attributes are just for explaining)
Table Client
id
company_name
address
Table Worker
id
first_name
last name
Table Contact
id
client_id
worker_id
my models looks like these
class Worker < ActiveRecord::Base
has_many :contacts, :dependent => :destroy
has_many :clients, :through => :contacts, :foreign_key => 'client_id'
end
class Client < ActiveRecord::Base
has_many :contacts, :foreign_key => "client_id",:dependent => :destroy
has_many :workers, :through => :contacts, :foreign_key => 'worker_id'
accepts_nested_attributes_for :workers, :allow_destroy => false
end
class Contact < ActiveRecord::Base
belongs_to :worker, :foreign_key => "worker_id"
belongs_to :client, :foreign_key => "client_id"
end
Then in my form for create a client, I can create many workers, and rails make the association and creates the instances for the contacts table (by using nested forms).
The thing is, if I want to edit a client by removing a contact, the contact is not removed. As you can see I put this line in the clients model
accepts_nested_attributes_for :workers, :allow_destroy => false
I set the allow_destroy to false, because I don't want to delete the worker itself, I just want to remove the contact tuple.
Does anybody know how can I solve this?? Hope you can help me... Thanks
I've got the following setup:
class Vote < ActiveRecord::Base
belongs_to :voteable, :polymorphic => :true, :counter_cache => true
end
class Proposition < ActiveRecord::Base
has_many :votes, :as => :voteable
end
class Winner < ActiveRecord::Base
has_many :votes, :as => :voteable
end
The Vote table looks like this:
t.string "ip_address"
t.integer "voteable_id"
t.string "voteable_type"
I want to validate the following. A user with a given ip_address can only vote on 1 proposition. So the combination of ip_address, voteable_id and voteable_type needs to be unique.
How can i achieve this with a "simple" validation rule?
To guarantee uniqueness you have to add unique index to your DB
If you don't have important data yet you can do it inside migration with add_index
add_index(:votes, [:ip_address, :voteable_id, voteable_type], :unique => true, :name => 'allowed_one_vote')
in case you already have some data it can be done with SQL and it depends on your DBMS
Add a scope to a unique :ip_address validation
class Vote < ActiveRecord::Base
# ...
validates :ip_address, uniqueness: { scope: [:voteable_type, :voteable_id]}
end
I have the following set of models:
class Cardstock < ActiveRecord::Base
has_many :color_matches, :primary_key => :hex, :foreign_key => :hex
has_many :palette_colors, :through => :color_matches
end
class ColorMatch < ActiveRecord::Base
belongs_to :palette_color
has_many :cardstocks, :foreign_key => :hex, :primary_key => :hex
end
class PaletteColor < ActiveRecord::Base
has_many :color_matches
has_many :cardstocks, :through => :color_matches
end
Calling Cardstock.last.palette_colors yields the following error:
ActiveRecord::StatementInvalid: PGError: ERROR: operator does not exist: character varying = integer
LINE 1: ...".palette_color_id WHERE (("color_matches".hex = 66)) OR...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "palette_colors".* FROM "palette_colors" INNER JOIN "color_matches" ON "palette_colors".id = "color_matches".palette_color_id WHERE (("color_matches".hex = 66)) ORDER BY name ASC
This shows me that the query ActiveRecord generates is using the cardstock's id (66) where it should be using the cardstock's hex (bbbbaf). Somewhere, I need to specify to ActiveRecord to use the hex column to join between cardstocks and color_matches. Does ActiveRecord support this?
Your relationships are all out of whack here.
the relationships between Cardstock and ColorMatch should be a has_and_belongs_to_many relationship on both sides
anywhere you have a has_many relationship, you need a corresponding belongs_to relationship in the corresponding class
There's something wrong with the way your relationships are set up. I don't quite understand your specific use case here, so I'm not sure where the problem is. The way to think about this is probably as a many-to-many relationship. Figure out what the two sides of that many-to-many are, and what's the join model. I'm going to give an example assuming that ColorMatch is your join model -- it's what relates a PaletteColor to a Cardstock. In that case, you'll want your relationships to look something like this:
class Cardstock < ActiveRecord::Base
has_many :color_matches, :primary_key => :hex, :foreign_key => :hex
has_many :palette_colors, :through => :color_matches
end
class ColorMatch < ActiveRecord::Base
belongs_to :palette_color
belongs_to :cardstocks, :foreign_key => :hex, :primary_key => :hex
end
class PaletteColor < ActiveRecord::Base
has_many :color_matches
has_many :cardstocks, :through => :color_matches
end
In terms of your database, you should have a palette_color_id and a hex field on the color_matches table, and a hex field on the cardstocks table.