Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

No method error when I try to use create or new #65

Closed
vsadhu opened this Issue · 10 comments

2 participants

@vsadhu

NoMethodError (undefined method `new' for #FileOrFolder:0x)

code :

newStructure = FileOrFolder.new
newStructure.fullpath = path
pathbits = path.split('/')
newStructure.name = pathbits.last

grandparent = newStructure.new(:name => "Grandparent")
@vsadhu

private method `create' called for #FileOrFolder:008034 error ...when i try to use create method

@mceachen
Owner
@vsadhu

even if i save newStructure and then use newStructure.new ...i get the same error ........basically I am trying to replace awesome nested tree with this gem due to the performance issues ...so i have newStructure.move_to_child_of(parent); and i wish to replace it with the corresponding code in closure_tree so replaced it with newStructure.find_or_create_by_path(parent); ....but it doesnt work ...gives me an error "undefined method `empty?' for #FileOrFolder:0x00000005e24d98" ... detailed explanation of the code is here .... http://stackoverflow.com/questions/17281285/using-closure-tree-gem-instead-of-awesome-nested-set-please-help-me-with-th?noredirect=1#comment25055492_17281285

@vsadhu

newStructure.find_or_create_by_path(parent); seems to add entries to the table but .... in the hierarchy table generations column is all zeros ..... and the parent_id column in table for model is zero ..... any possible reason for this ?? what is the alternative way of using newStructure.move_to_child_of(parent); in closure_tree ?? Thanks for your time :)

@mceachen
Owner

I'd need to see your database structure to help more. I've submitted an answer to stackoverflow (please accept my answer), but also, make sure you look at the example models in the spec directory.

@mceachen mceachen closed this
@vsadhu

Thanks for the reply. ...I made the changes you mentioned..and the entries are being added as expected, just that fullpath is not being populated correctly ..... this column is empty in the table for all the entries...could you please help, I have edited my post on stackoverflow .... http://stackoverflow.com/questions/17281285/using-closure-tree-gem-instead-of-awesome-nested-set-please-help-me-with-th/17303157?noredirect=1#17303157

@vsadhu

I could implement closure_tree ...finally ...but its taking a lot of time than awesome_nested_set for retrieval ..... awesome_nested_set was more fast than this gem :( ....i am not sure if am missing something ..functionality is working as expected ..but performance wise its very very slow...

@vsadhu

FileOrFolder Load (14560.8ms)

SELECT `file_or_folders`.* FROM `file_or_folders` INNER JOIN `file_or_folder_hierarchies` ON `file_or_folders`.`id` = `file_or_folder_hierarchies`.`descendant_id` INNER JOIN (
 SELECT ancestor_id
 FROM `file_or_folder_hierarchies`
 GROUP BY 1
 HAVING MAX(`file_or_folder_hierarchies`.generations) = 0
 ) AS leaves ON (`file_or_folders`.id = leaves.ancestor_id) WHERE `file_or_folder_hierarchies`.`ancestor_id` = 147 ORDER BY `file_or_folder_hierarchies`.generations asc
  EXPLAIN (13343.7ms)  EXPLAIN SELECT `file_or_folders`.* FROM `file_or_folders` INNER JOIN `file_or_folder_hierarchies` ON `file_or_folders`.`id` = `file_or_folder_hierarchies`.`descendant_id` INNER JOIN (
 SELECT ancestor_id
 FROM `file_or_folder_hierarchies`
 GROUP BY 1
 HAVING MAX(`file_or_folder_hierarchies`.generations) = 0
 ) AS leaves ON (`file_or_folders`.id = leaves.ancestor_id) WHERE `file_or_folder_hierarchies`.`ancestor_id` = 147 ORDER BY `file_or_folder_hierarchies`.generations asc

EXPLAIN for: SELECT file_or_folders.* FROM file_or_folders INNER JOIN file_or_folder_hierarchies ON file_or_folders.id = file_or_folder_hierarchies.descendant_id INNER JOIN (
SELECT ancestor_id
FROM file_or_folder_hierarchies
GROUP BY 1
HAVING MAX(file_or_folder_hierarchies.generations) = 0
) AS leaves ON (file_or_folders.id = leaves.ancestor_id) WHERE file_or_folder_hierarchies.ancestor_id = 147 ORDER BY file_or_folder_hierarchies.generations asc

+----+-------------+----------------------------+--------+------------------------------------------------------------------------------------+----------------------------------+---------+--------------------+---------+---------------------------------+
| id | select_type | table                      | type   | possible_keys                                                                      | key                                  | key_len | ref                | rows    | Extra                           |
+----+-------------+----------------------------+--------+------------------------------------------------------------------------------------+----------------------------------+---------+--------------------+---------+---------------------------------+
 |  1 | PRIMARY     | file_or_folder_hierarchies | ref    | index_file_or_folders_on_ans_des,index_file_or_folder_hierarchies_on_descendant_id | index_file_or_folders_on_ans_des | 4       | const              |      15 | Using temporary; Using filesort |
|  1 | PRIMARY     | <derived2>                 | ALL    | NULL                                                                               | NULL                             | NULL    | NULL               |  104704 | Using where; Using join buffer  |
 |  1 | PRIMARY     | file_or_folders            | eq_ref | PRIMARY                                                                            | PRIMARY                          | 4       | leaves.ancestor_id |       1 | Using where                     |
 |  2 | DERIVED     | file_or_folder_hierarchies | index  | NULL                                                                               | index_file_or_folders_on_ans_des | 8       | NULL               | 1340096 |                                 |
 +----+-------------+----------------------------+--------+------------------------------------------------------------------------------------+----------------------------------+---------+--------------------+---------+---------------------------------+
@mceachen
Owner

It looks like you're missing some critical indicies, but I'm not going to be able to give you any more personal support.

If you find an issue, pull requests are welcome.

@vsadhu

Hi Mceachen ... I dont find any help on this gem else where .I have implemented this gem in to my project already . it would be great if you could help me out pls.
my hierarchy table is defined like this .... I had to give the name parameter for index as the length of name was exceeding the limit.

def change
create_table :file_or_folder_hierarchies do |t|
t.integer :ancestor_id, :null => false # ID of the parent/grandparent/great-grandparent/... tag
t.integer :descendant_id, :null => false # ID of the target tag
t.integer :generations, :null => false # Number of generations between the ancestor and the descendant. Parent/child = 1, for example.
end
# For "all progeny of…" selects:
add_index :file_or_folder_hierarchies, [:ancestor_id, :descendant_id],:name => "index_file_or_folders_on_ans_des", :unique => true

# For "all ancestors of…" selects
add_index :file_or_folder_hierarchies, [:descendant_id]
end

and my file_or_folder is defined like this :
def change
# create file_or_folders
create_table :file_or_folders do |t|
t.column :parent_id, :int
t.column :lft, :int
t.column :rgt, :int
t.column :fullpath, :string
t.column :name, :string
t.timestamps
end

add_index :file_or_folders, :fullpath, :name => "index_file_or_folders_on_fullpath"
add_index :file_or_folders, :lft, :name => "index_file_or_folders_on_lft"
add_index :file_or_folders, :name, :name => "index_file_or_folders_on_name"
add_index :file_or_folders, :parent_id, :name => "index_file_or_folders_on_parent_id"
add_index :file_or_folders, :rgt, :name => "index_file_or_folders_on_rgt"

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.