Search Results

Search found 73 results on 3 pages for 'habtm'.

Page 2/3 | < Previous Page | 1 2 3  | Next Page >

  • ActiveRecord habtm through belongs_to

    - by phillee
    Setup: ModelA <- habtm -> ModelB <- has_many / belongs_to -> ModelC I'd like to setup a habtm between ModelB and ModelC, I've tried has_many :ModelA, :through => :Model3 But that doesn't seem to work. Is there a way to accomplish this?

    Read the article

  • Using HABTM relationships in cakephp plugins with unique set to false

    - by Dean
    I am working on a plugin for our CakePHP CMS that will handle blogs. When getting to the tags I needed to set the HABTM relationship to unique = false to be able add tags to a post without having to reset them all. The BlogPost model looks like this class BlogPost extends AppModel { var $name = 'BlogPost'; var $actsAs = array('Core.WhoDidIt', 'Containable'); var $hasMany = array('Blog.BlogPostComment'); var $hasAndBelongsToMany = array('Blog.BlogTag' => array('unique' => false), 'Blog.BlogCategory'); } The BlogTag model looks like this class BlogTag extends AppModel { var $name = 'BlogTag'; var $actsAs = array('Containable'); var $hasAndBelongsToMany = array('Blog.BlogPost'); } The SQL error I am getting when I have the unique = true setting in the HABTM relationship between the BlogPost and BlogTag is Query: SELECT `Blog`.`BlogTag`.`id`, `Blog`.`BlogTag`.`name`, `Blog`.`BlogTag`.`slug`, `Blog`.`BlogTag`.`created_by`, `Blog`.`BlogTag`.`modified_by`, `Blog`.`BlogTag`.`created`, `Blog`.`BlogTag`.`modified`, `BlogPostsBlogTag`.`blog_post_id`, `BlogPostsBlogTag`.`blog_tag_id` FROM `blog_tags` AS `Blog`.`BlogTag` JOIN `blog_posts_blog_tags` AS `BlogPostsBlogTag` ON (`BlogPostsBlogTag`.`blog_post_id` = 4 AND `BlogPostsBlogTag`.`blog_tag_id` = `Blog`.`BlogTag`.`id`) As you can see it is trying to set the blog_tags table to 'Blog'.'BlogTag. which isn't a valid MySQL name. When I remove the unique = true from the relationship it all works find and I can save one tag but when adding another it just erases the first one and puts the new one in its place. Does anyone have any ideas? is it a bug or am I just missing something? Cheers, Dean

    Read the article

  • Is there a way to have three way habtm associations in rails / activerecord?

    - by txwikinger
    Often three (or more) way associations are needed for habtm associations. For instance a permission model with roles. A particular area of functionality has many users which can access many areas. The permissions on the area are setup via roles (habtm) The user/roles association is also habtm The permissions (read, write, delete, etc) are habtm towards roles. How would that be best done with rails/activerecord?

    Read the article

  • has_many through a habtm relationship in Rails

    - by macek
    I'm trying to define a has_many X, :through => Y where Y is a habtm relationship. Rails is throwing a fit about this. See comment in user model: class User < ActiveRecord::Base has_many :posts # I want to display a list of all tags this user is involved in has_many :tags, :through => :posts # ERROR end class Post < ActiveRecord::Base has_and_belongs_to_many :tags end class Tag < ActiveRecord::Base has_and_belongs_to_many :posts end What can I do to fix this?

    Read the article

  • validates_uniqueness_of with HABTM relationship

    - by jeffshantz
    I've got a HABTM relationship between two models: Publication and Author. I want to ensure that one cannot create a publication with the same title, year, and author list. However, if I try something like this: class Publication < ActiveRecord::Base validates_uniqueness_of :title, :scope => [:year, :authors] end This obviously won't work since there is no authors column. Can this be done with validates_uniqueness_of, or do I need a custom validator? Thank you.

    Read the article

  • CakePHP HABTM Plugin table naming conventions (for 1.3)

    - by Parris
    Hi everyone, I know naming conventions for tables used by plugins generally start with the name of the plugin and then the model pluralized. For example lets say I had a plugin called Poll, with a model also called PollPoll and another model called PollTag then the resulting table names would be poll_polls and poll_tags. They would also have a habtm relationship so what is the convention for that table name? I believe it would poll_poll_polls_poll_tags, although it is a little redundant it makes sense since the first poll_ represents the name of the plugin, while poll_polls and poll_tags relates to the models. Also have any naming conventions changed for plugins in 1.3? Is the above stated correct? Thanks!

    Read the article

  • getting hasMany records of a HABTM relationship

    - by charliefarley321
    I have tables: categories HABTM sculptures hasMany images from the CategoriesController#find() produces an array like so: array( 'Category' => array( 'id' => '3', 'name' => 'Modern', ), 'Sculpture' => array( (int) 0 => array( 'id' => '25', 'name' => 'Ami', 'material' => 'Bronze', 'CategoriesSculpture' => array( 'id' => '18', 'category_id' => '3', 'sculpture_id' => '25' ) ), (int) 1 => array( 'id' => '26', 'name' => 'Charis', 'material' => 'Bronze', 'CategoriesSculpture' => array( 'id' => '19', 'category_id' => '3', 'sculpture_id' => '26' ) ) ) ) I'd like to be able to get the images related to sculpture in the array as well if this is possible?

    Read the article

  • CakePHP - Sorting using HABTM Join Table Field

    - by Ashok
    Hello Cake Gurus, here's my problem: Table1: Posts id - int title - varchar Table2: Categories id - int name - varchar HABTM JoinTable: categories_posts id - int post_id - int category_id - int postorder - int As you can see, the join table contains a field called 'postorder' - This is for ordering the posts in a particular category. For example, Posts: Post1, Post2, Post3, Post4 Categories: Cat1, Cat2 Ordering: Cat1 - Post1, Post3, Post2 Cat2 - Post3, Post1, Post4 Now in CakePHP, $postpages = $this->Post->Category->find('all'); gives me a array like Array ( [0] => Array ( [Category] => Array ( [id] => 13 [name] => Cat1 ) [Post] => Array ( [0] => Array ( [id] => 1 [title] => Post2 [CategoriesPost] => Array ( [id] => 17 [post_id] => 1 [category_id] => 13 [postorder] => 3 ) ) [1] => Array ( [id] => 4 [title] => Post1 [CategoriesPost] => Array ( [id] => 21 [post_id] => 4 [category_id] => 13 [postorder] => 1 ) ) ) ) ) As you can see [Post], they are not ordered according to [CategoriesPost].postorder but are ordered according to [CategoriesPost].id. How can I get the array ordered according to [CategoriesPost].postorder? Thanks in advance for your time :) Edit: The Queries from Cake's SQL Log are: SELECT `Category`.`id`, `Category`.`name` FROM `categories` AS `Category` WHERE 1 = 1 SELECT `Post`.`id`, `Post`.`title`, `CategoriesPost`.`id`, `CategoriesPost`.`post_id`, `CategoriesPost`.`category_id`, `CategoriesPost`.`postorder` FROM `posts` AS `Post` JOIN `categories_posts` AS `CategoriesPost` ON (`CategoriesPost`.`category_id` IN (13, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52) AND `CategoriesPost`.`post_id` = `Post`.`id`) What I am looking for is how to make cake put a Order By CategoriesPost.postorder in that second SELECT SQL Query.

    Read the article

  • CakePHP pagination with HABTM models

    - by nickf
    I'm having some problems with creating pagination with a HABTM relationship. First, the tables and relationships: requests (id, to_location_id, from_location_id) locations (id, name) items_locations (id, item_id, location_id) items (id, name) So, a Request has a Location the request is coming from and a Location the Request is going to. For this question, I'm only concerned about the "to" location. Request --belongsTo--> Location* --hasAndBelongsToMany--> Item (* as "ToLocation") In my RequestController, I want to paginate all the Items in a Request's ToLocation. // RequestsController var $paginate = array( 'Item' => array( 'limit' => 5, 'contain' => array( "Location" ) ) ); // RequestController::add() $locationId = 21; $items = $this->paginate('Item', array( "Location.id" => $locationId )); And this is failing, because it is generating this SQL: SELECT COUNT(*) AS count FROM items Item WHERE Location.id = 21 I can't figure out how to make it actually use the "contain" argument of $paginate... Any ideas?

    Read the article

  • Rails HABTM accepts_nested_attributes_for mapping table

    - by Rabbott
    Currently I have a habtm relationship between a datastream (stream), and a chart. I want to be able to use the 'typical' jquery "add new stream" method to add a new entry into a mapping table that holds the chart_id, and stream_id.. But I think that most examples out there are made to handle a one to many relationship.. The functionality provided here by Ryan Bates is what I'm looking for, But I dont want to use checkboxes, I want to use a select (drop down) to add new items to the mapping table. I need THIS and THIS combined chart.rb class Chart < ActiveRecord::Base has_and_belongs_to_many :streams, :readonly => false, :join_table => 'charts_streams' accepts_nested_attributes_for :streams, :reject_if => lambda { |a| a.values.all?(&:blank?) }, :allow_destroy => true stream.rb class Stream < ActiveRecord::Base has_and_belongs_to_many :charts, :readonly => false, :join_table => 'charts_streams' application.js /* * Method used to add new child form partials */ $('form a.add_child').click(function() { var assoc = $(this).attr('data-association'); var content = $('#' + assoc + '_fields_template').html(); var regexp = new RegExp('new_' + assoc, 'g'); var new_id = new Date().getTime(); var newElements = jQuery(content.replace(regexp, new_id)).hide(); $(this).parent().before(newElements).prev().slideFadeToggle(); return false; }); /* * Method used to remove child form partials */ $('form a.remove_child').live('click', function() { if(confirm('Are you sure?')) { var hidden_field = $(this).prev('input[type=hidden]')[0]; if(hidden_field) { hidden_field.value = '1'; } $(this).parents('.fields').slideFadeToggle(); } return false; }); _form.html.erb <div id="streams"> <h4>Streams</h4> <% form.fields_for :streams do |stream_form| %> <%= render :partial => "stream", :locals => {:f => stream_form} %> <% end %> </div> <p><%= add_child_link 'Add a stream', :streams %></p> <%= new_child_fields_template(form, :streams) %> _stream.html.erb <div class="fields"> <p> <%= f.label :stream_id %><br /> <%= select_tag "chart[stream_ids][]", options_for_select(@streams.map {|s| [s.label, s.id]}, f.object.id) %> </p> <p><%= remove_child_link 'Remove stream', f %></p> </div> This may be overkill and what I am looking to do could be much easier.. Basically when I create a chart I want to be able to add as many streams to it as I want, using the javascript for adding a new entry, and removing an old one... Thanks for the help! This is 'working' but gives me a ActiveRecord::ReadOnlyRecord error when it hits the chart.update_attributes method.. am I adding the the :readonly = false in the wrong spot? Or am I just doing it completely wrong?

    Read the article

  • bidirectional habtm linking

    - by Alexey Poimtsev
    Hi, all. I have application with 2 groups of models - content based (news, questions) and "something" based (devices, applications etc). I need to link all models between groups - for example question may belongs to 3 different things - one application and 2 devices. The same - for news. From other side - i need to see all news articles and questions related to some application or device. Any idea how to develop this in rails? I have only one idea - mixins that will add methods content_id and thing_id to models and join table.

    Read the article

  • set a default saving data in a selection box in HABTM model

    - by vincent low
    here is my problem which in my system. my system allow user to add event, which include date, time, places. In the other hand, user are allow to choose the "Event sharing to" some of other user. So i successful to created and share to other user, when the user login which are being choose to share with the event, he is able to view for that particular event. But the problem is, when user add the event, he must choose for his own name also in the select box. If not he will be unable to read the event that he had created. So i need to save a default data to my model, which is whenever the user got choose the sharing event or not, it also will save the data to his user id. here is my code for select box, what can i edit and do to let it set a default saving value which is always save with the user own data?? input('User',array( 'label' = 'Select Related Potential', 'options' = $users, //'id'='user', 'style'='width:250px;height:100px', //'selected' = $ownUserId )); ? i try to solve by adding 1 more row to the add.ctp, but the permission just set to the own user who created it, the other user i choose is unable to read. $form-input('User',array( 'label' = 'Select Related Potential', 'options' = $users, //'id'='user', 'style'='width:250px;height:100px', 'selected' = $ownUserId )); $form-input('User',array('type'='hidden','value'=$ownUserId));

    Read the article

  • Cakephp, i18n, Retrieve translation records for associated models

    - by ion
    Quoting from the cakephp Book (ver 1.3): Note that only fields of the model you are directly doing find on will be translated. Models attached via associations won't be translated because triggering callbacks on associated models is currently not supported. Has anyone come up with a solution for this??? If not could you give me some pointers concerning the following simple scenario. I have 2 models: Project, Category. Project HABTM Category I have properly set up i18n table and I have a few entries in the db, all translated. When I retrieve a project it does retrieve the translation but not the translated category because as it says in the cakephp book models attached via associations won't be translated. Any help would be appreciated

    Read the article

  • Ruby on Rails: How to find all items with a hash that contain a specific value...

    - by kingjeffrey
    Suppose I have three models: Student, SchoolClass, and DayOfWeek. There is a HABTM relationship between Student and SchoolClass, and between SchoolClass and DayOfWeek. What I'd like to do is find all school classes belonging to a given student that meet on Monday. Now I suppose I could do something like: @student = Student.find(:student_id) @student_classes = @student.school_classes.find(:all) @student_classes_on_monday = Array.new @student_classes.each do |student_class| if student_class.day_of_week.include?("Monday") @student_classes_on_monday << student_class end end But there has to be a more elegant way. Can you help me find it?

    Read the article

  • Rails has_and_belongs_to_many relationship question

    - by Kevin Whitaker
    Hello all, I'm sure that this question has been asked somewhere before, as the habtm relationship seems to be very confusing. I have two models, users and promotions. The idea is that a promotion can have many users, and a user can have many promotions. class User < ActiveRecord::Base has_and_belongs_to_many :promotions end class Promotion < ActiveRecord::Base has_and_belongs_to_many :users end I also have a promotions_users table/model, with no id of its own. It references user_id and promotions_id class PromotionsUsers < ActiveRecord::Base end So, how do I add a user to a promotion? I've tried something like this: user = User.find(params[:id]) promotion = Promotion.find(params[:promo_id]) promo = user.promotions.new(promo) This results in the following error: NoMethodError: undefined method `stringify_keys!' for #<Promotion:0x10514d420> If I try this line instead: promo= user.promotions.new(promo.id) I get this error: TypeError: can't dup Fixnum I'm sure that there is a very easy solution to my problem, and I'm just not searching for the solution the right way. Thank you for your time, and any help you can provide.

    Read the article

  • Rails and Searchlogic: finding products that matching all given product categories by using searchlo

    - by Roland
    I have a model Publication and a model Category in my Rails app. Both are connected with a has_and_belongs_to_many association. Now I would like to search publications that match one or more categories. If more than one category is given they have all assigned to the publication. I want to specify the categories in a multiple select_box. Publication.released.categories_id_is([1,2]) is not working because the categories are connected with OR. With Publication.categories_id_is_all([1,2]) the categories are connected with AND, but no result is given back. Any idea's on that? Am I mising the right point in the docs. Thanks for your very welcome help!

    Read the article

  • many to many query for ActiveRecord

    - by JP
    I have three data models, Users, Conversations and Lines, where each conversation has many lines and certain participating users, each line has one conversation and one user and each user has many conversations and many lines. I have arranged these in ActiveRecord like this: class Line < ActiveRecord::Base belongs_to :conversation belongs_to :user end class User < ActiveRecord::Base has_and_belongs_to_many :conversations has_many :lines end class Conversation < ActiveRecord::Base has_many :lines has_and_belongs_to_many :users end If I want to create a new conversation with 4 users, where the users are either found or created inside the users table, how would I go about doing this? I thought I could do: c = Conversation.new c.users.find_or_create_by_username('myUsername') c.save But this will create a new username in the Users table even if that username already exists! (ie. running the above code 3 times will result in Users having 3 rows with 'myUsername' as the username, one for each conversation, rather than three conversations all with the same 'myUsername' entry listed in their associated users) I'm not sure how to search for this kind of information with google - can anyone help?

    Read the article

  • Saving a Join Model

    - by Thorpe Obazee
    I've been reading the cookbook for a while now and still don't get how I'm supposed to do this: My original problem was this: A related Model isn't being validated From RabidFire's commment: If you want to count the number of Category models that a new Post is associated with (on save), then you need to do this in the beforeSave function as I've mentioned. As you've currently set up your models, you don't need to use the multiple rule anywhere. If you really, really want to validate against a list of Category IDs for some reason, then create a join model, and validate category_id with the multiple rule there. Now, I have these models and are now validating. The problem now is that data isn't being saved in the Join Table: class Post extends AppModel { var $name = 'Post'; var $hasMany = array( 'CategoryPost' => array( 'className' => 'CategoryPost' ) ); var $belongsTo = array( 'Page' => array( 'className' => 'Page' ) ); class Category extends AppModel { var $name = 'Category'; var $hasMany = array( 'CategoryPost' => array( 'className' => 'CategoryPost' ) ); class CategoryPost extends AppModel { var $name = 'CategoryPost'; var $validate = array( 'category_id' => array( 'rule' => array('multiple', array('in' => array(1, 2, 3, 4))), 'required' => FALSE, 'message' => 'Please select one, two or three options' ) ); var $belongsTo = array( 'Post' => array( 'className' => 'Post' ), 'Category' => array( 'className' => 'Category' ) ); This is the new Form: <div id="content-wrap"> <div id="main"> <h2>Add Post</h2> <?php echo $this->Session->flash();?> <div> <?php echo $this->Form->create('Post'); echo $this->Form->input('Post.title'); echo $this->Form->input('CategoryPost.category_id', array('multiple' => 'checkbox')); echo $this->Form->input('Post.body', array('rows' => '3')); echo $this->Form->input('Page.meta_keywords'); echo $this->Form->input('Page.meta_description'); echo $this->Form->end('Save Post'); ?> </div> <!-- main ends --> </div> The data I am producing from the form is as follows: Array ( [Post] => Array ( [title] => 1234 [body] => 1234 ) [CategoryPost] => Array ( [category_id] => Array ( [0] => 1 [1] => 2 ) ) [Page] => Array ( [meta_keywords] => 1234 [meta_description] => 1234 [title] => 1234 [layout] => index ) ) UPDATE: controller action //Controller action function admin_add() { // pr(Debugger::trace()); $this->set('categories', $this->Post->CategoryPost->Category->find('list')); if ( ! empty($this->data)) { $this->data['Page']['title'] = $this->data['Post']['title']; $this->data['Page']['layout'] = 'index'; debug($this->data); if ($this->Post->saveAll($this->data)) { $this->Session->setFlash('Your post has been saved', 'flash_good'); $this->redirect($this->here); } } } UPDATE #2: Should I just do this manually? The problem is that the join tables doesn't have things saved in it. Is there something I'm missing? UPDATE #3 RabidFire gave me a solution. I already did this before and am quite surprised as so why it didn't work. Thus, me asking here. The reason I think there is something wrong. I don't know where: Post beforeSave: function beforeSave() { if (empty($this->id)) { $this->data[$this->name]['uri'] = $this->getUniqueUrl($this->data[$this->name]['title']); } if (isset($this->data['CategoryPost']['category_id']) && is_array($this->data['CategoryPost']['category_id'])) { echo 'test'; $categoryPosts = array(); foreach ($this->data['CategoryPost']['category_id'] as $categoryId) { $categoryPost = array( 'category_id' => $categoryId ); array_push($categoryPosts, $categoryPost); } $this->data['CategoryPost'] = $categoryPosts; } debug($this->data); // Gives RabidFire's correct array for saving. return true; } My Post action: function admin_add() { // pr(Debugger::trace()); $this->set('categories', $this->Post->CategoryPost->Category->find('list')); if ( ! empty($this->data)) { $this->data['Page']['title'] = $this->data['Post']['title']; $this->data['Page']['layout'] = 'index'; debug($this->data); // First debug is giving the correct array as above. if ($this->Post->saveAll($this->data)) { debug($this->data); // STILL gives the above array. which shouldn't be because of the beforeSave in the Post Model // $this->Session->setFlash('Your post has been saved', 'flash_good'); // $this->redirect($this->here); } } }

    Read the article

  • How do I do multiple has_and_belongs_to_many associations between the same two classes?

    - by Ermin
    I have the following setup: class Publication < ActiveRecord::Base has_and_belongs_to_many :authors, :class_name=>'Person', :join_table => 'authors_publications' has_and_belongs_to_many :editors, :class_name=>'Person', :join_table => 'editors_publications' end class Person < ActiveRecord::Base has_and_belongs_to_many :publications end With this setup I can do stuff like Publication.first.authors. But if I want to list all publications in which a person is involved Person.first.publications, an error about a missing join table people_publications it thrown. How could I fix that? Should I maybe switch to separate models for authors and editors? It would however introduce some redundancy to the database, since a person can be an author of one publication and an editor of another.

    Read the article

  • Does CakePHP treat all INT fields as ID's for join tables?

    - by Jonnie
    I am trying to save a User, their Profile, and some tags and my join table that links the profile and the tags keeps getting messed up. The profile model is called Instructor, the tag model is called Subject. The Instructor has a phone number and a zip code and for some reason CakePHP thinks these are the fields it should use when creating entries in my join table. My Join table always comes out as: id | instructor_id | subject_id | 1 | 90210 | 1 | // thinks that the zip code is an instructor_id 2 | 1112223333 | 1 | // thinks that the phone number is an instructor_id 3 | 1 | 1 | // thinks that user_id is an instructor_id 4 | 1 | 1 | // the actual instructor_id, this one's correct 5 | 90210 | 2 | 6 | 1112223333 | 2 | 3 | 1 | 2 | 4 | 1 | 2 | My Models: class Instructor extends AppModel { var $name = 'Instructor'; var $belongsTo = array('User', 'State'); var $hasAndBelongsToMany = array( 'Subject' = array( 'className' = 'Subject', 'joinTable' = 'instructors_subjects', 'foreignKey' = 'instructor_id', 'associationForeignKey' = 'subject_id', 'unique' = true, 'conditions' = '', 'fields' = '', 'order' = '', 'limit' = '', 'offset' = '', 'finderQuery' = '', 'deleteQuery' = '', 'insertQuery' = '' ) ); } class Subject extends AppModel { var $name = 'Subject'; var $hasAndBelongsToMany = array( 'Instructor' = array( 'className' = 'Instructor', 'joinTable' = 'instructors_subjects', 'foreignKey' = 'subject_id', 'associationForeignKey' = 'instructor_id', 'unique' = true, 'conditions' = '', 'fields' = '', 'order' = '', 'limit' = '', 'offset' = '', 'finderQuery' = '', 'deleteQuery' = '', 'insertQuery' = '' ) ); } My Model Associations: User hasOne Instructor Instructor belongsTo User Instructor hasAndBelongsToMany Subject Subject hasAndBelongsToMany Instructor My form data looks like: Array ( [User] = Array ( [username] = MrInstructor [password] = cddb06c93c72f34eb9408610529a34645c29c55d [group_id] = 2 ) [Instructor] = Array ( [name] = Jimmy Bob [email] = [email protected] [phone] = 1112223333 [city] = Beverly Hills [zip_code] = 90210 [states] = 5 [website] = www.jimmybobbaseballschool.com [description] = Jimmy Bob is an instructor. [user_id] = 1 [id] = 1 ) [Subject] = Array ( [name] = hitting, pitching ) ) My function for processing the form looks like: function instructor_register() { $this-set('groups', $this-User-Group-find('list')); $this-set('states', $this-User-Instructor-State-find('list')); if (!empty($this-data)) { // Set the group to Instructor $this-data['User']['group_id'] = 2; // Save the user data $user = $this-User-save($this-data, true, array( 'username', 'password', 'group_id' )); // If the user was saved, save the instructor's info if (!empty($user)) { $this-data['Instructor']['user_id'] = $this-User-id; $instructor = $this-User-Instructor-save($this-data, true, array( 'user_id', 'name', 'email', 'phone', 'city', 'zip_code', 'state_id', 'website', 'description' )); // If the instructor was saved, save the rest if(!empty($instructor)) { $instructorId = $this-User-Instructor-id; $this-data['Instructor']['id'] = $instructorId; // Save each subject seperately $subjects = explode(",", $this-data['Subject']['name']); foreach ($subjects as $_subject) { // Get the correct subject format $_subject = strtolower(trim($_subject)); $this-User-Instructor-Subject-create($this-data); $this-User-Instructor-Subject-set(array( 'name' = $_subject )); $this-User-Instructor-Subject-save(); echo ''; print_r($this-data); echo ''; } } } } }

    Read the article

  • Rails + simple role system through associative table

    - by user202411
    So I have the Ninja model which has many Hovercrafts through ninja_hovercrafts (which stores the ninja_id and the hovercraft_id). It is of my understanding that this kind of arrangement should be set in a way that the associative table stores only enough information to bind two different classes. But I'd like to use the associative table to work as a very streamlined authorization hub on my application. So i'd also like this table to inform my system if this binding makes the ninja the pilot or co-pilot of a given hovercraft, through a "role" field in the table. My questions are: Is this ugly? Is this normal? Are there methods built into rails that would help me to automagically create Ninjas and Hovercrafts associations WITH the role? For exemple, could I have a nested form to create both ninjas and hcs in a way that the role field in ninjas_hovercrafts would be also filled? If managing my application roles this way isn't a good idea, whats the non-resource heavy alternative (my app is being designed trying to avoid scalability problems such as excessive joins, includes, etc) thank you

    Read the article

  • join same rails models twice, eg people has_many clubs through membership AND people has_many clubs through committee

    - by Ben
    Models: * Person * Club Relationships * Membership * Committee People should be able to join a club (Membership) People should be able to be on the board of a club (Committee) For my application these involve vastly different features, so I would prefer not to use a flag to set (is_board_member) or similar. I find myself wanting to write: People has_many :clubs :through = :membership # :as = :member? :foreign_key = :member_id? has_many :clubs :through = :committee # as (above) but I'm not really sure how to stitch this together

    Read the article

  • How to create a view to manage associations between HABTM models? (Rails)

    - by Chris Hart
    Hello, I am using Ruby on Rails and need to create a view that allows the creation of records through a HABTM relationship to another model. Specifically, I have the following models: Customer and ServiceOverride, and a join table customers_serviceoverrides. Using the customer view for create/update, I need to be able to create, update and delete ServiceOverrides and manage the attributes of the associated model(s) from the same view. Visually I'd prefer to have something like a plus/minus sign to add/delete service overrides, and each serviceoverride record has two string entities which need to be displayed and editable as well. However, if I could just get the code (a kind of nested form, I'm assuming?) working, I could work out the UI aspects. The models are pretty simple: class ServiceOverride < ActiveRecord::Base has_and_belongs_to_many :customers end class Customer < ActiveRecord::Base has_and_belongs_to_many :serviceoverrides end The closest thing I've found explaining this online is on this blog but it doesn't really address what I'm trying to do (both manage the linkages to the other model, and edit attributes of that model. Any help is appreciated. Thanks in advance. Chris

    Read the article

< Previous Page | 1 2 3  | Next Page >